-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #6695 - TaKO8Ki:add-bytes-nth, r=phansch
New lint: `bytes_nth` This pull request adds a new lint named `bytes_nth`. --- closes: #6391 changelog: Added a new lint: `bytes_nth`
- Loading branch information
Showing
7 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::utils::{is_type_diagnostic_item, snippet_with_applicability, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::BYTES_NTH; | ||
|
||
pub(super) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>]) { | ||
if_chain! { | ||
if let ExprKind::MethodCall(_, _, ref args, _) = expr.kind; | ||
let ty = cx.typeck_results().expr_ty(&iter_args[0]).peel_refs(); | ||
let caller_type = if is_type_diagnostic_item(cx, ty, sym::string_type) { | ||
Some("String") | ||
} else if ty.is_str() { | ||
Some("str") | ||
} else { | ||
None | ||
}; | ||
if let Some(caller_type) = caller_type; | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
BYTES_NTH, | ||
expr.span, | ||
&format!("called `.byte().nth()` on a `{}`", caller_type), | ||
"try", | ||
format!( | ||
"{}.as_bytes().get({})", | ||
snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability), | ||
snippet_with_applicability(cx, args[1].span, "..", &mut applicability) | ||
), | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// run-rustfix | ||
|
||
#![allow(clippy::unnecessary_operation)] | ||
#![warn(clippy::bytes_nth)] | ||
|
||
fn main() { | ||
let s = String::from("String"); | ||
s.as_bytes().get(3); | ||
&s.as_bytes().get(3); | ||
s[..].as_bytes().get(3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// run-rustfix | ||
|
||
#![allow(clippy::unnecessary_operation)] | ||
#![warn(clippy::bytes_nth)] | ||
|
||
fn main() { | ||
let s = String::from("String"); | ||
s.bytes().nth(3); | ||
&s.bytes().nth(3); | ||
s[..].bytes().nth(3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: called `.byte().nth()` on a `String` | ||
--> $DIR/bytes_nth.rs:8:5 | ||
| | ||
LL | s.bytes().nth(3); | ||
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)` | ||
| | ||
= note: `-D clippy::bytes-nth` implied by `-D warnings` | ||
|
||
error: called `.byte().nth()` on a `String` | ||
--> $DIR/bytes_nth.rs:9:6 | ||
| | ||
LL | &s.bytes().nth(3); | ||
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)` | ||
|
||
error: called `.byte().nth()` on a `str` | ||
--> $DIR/bytes_nth.rs:10:5 | ||
| | ||
LL | s[..].bytes().nth(3); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `s[..].as_bytes().get(3)` | ||
|
||
error: aborting due to 3 previous errors | ||
|