-
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 #7292 - Jarcho:suspicious_splitn, r=flip1995
Add lint `suspicious_splitn` fixes: #7245 changelog: Add lint `suspicious_splitn`
- Loading branch information
Showing
9 changed files
with
197 additions
and
7 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
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,56 @@ | ||
use clippy_utils::consts::{constant, Constant}; | ||
use clippy_utils::diagnostics::span_lint_and_note; | ||
use if_chain::if_chain; | ||
use rustc_ast::LitKind; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Spanned; | ||
|
||
use super::SUSPICIOUS_SPLITN; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
method_name: &str, | ||
expr: &Expr<'_>, | ||
self_arg: &Expr<'_>, | ||
count_arg: &Expr<'_>, | ||
) { | ||
if_chain! { | ||
if let Some((Constant::Int(count), _)) = constant(cx, cx.typeck_results(), count_arg); | ||
if count <= 1; | ||
if let Some(call_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); | ||
if let Some(impl_id) = cx.tcx.impl_of_method(call_id); | ||
let lang_items = cx.tcx.lang_items(); | ||
if lang_items.slice_impl() == Some(impl_id) || lang_items.str_impl() == Some(impl_id); | ||
then { | ||
// Ignore empty slice and string literals when used with a literal count. | ||
if (matches!(self_arg.kind, ExprKind::Array([])) | ||
|| matches!(self_arg.kind, ExprKind::Lit(Spanned { node: LitKind::Str(s, _), .. }) if s.is_empty()) | ||
) && matches!(count_arg.kind, ExprKind::Lit(_)) | ||
{ | ||
return; | ||
} | ||
|
||
let (msg, note_msg) = if count == 0 { | ||
(format!("`{}` called with `0` splits", method_name), | ||
"the resulting iterator will always return `None`") | ||
} else { | ||
(format!("`{}` called with `1` split", method_name), | ||
if lang_items.slice_impl() == Some(impl_id) { | ||
"the resulting iterator will always return the entire slice followed by `None`" | ||
} else { | ||
"the resulting iterator will always return the entire string followed by `None`" | ||
}) | ||
}; | ||
|
||
span_lint_and_note( | ||
cx, | ||
SUSPICIOUS_SPLITN, | ||
expr.span, | ||
&msg, | ||
None, | ||
note_msg, | ||
); | ||
} | ||
} | ||
} |
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
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,20 @@ | ||
#![warn(clippy::suspicious_splitn)] | ||
|
||
fn main() { | ||
let _ = "a,b,c".splitn(3, ','); | ||
let _ = [0, 1, 2, 1, 3].splitn(3, |&x| x == 1); | ||
let _ = "".splitn(0, ','); | ||
let _ = [].splitn(0, |&x: &u32| x == 1); | ||
|
||
let _ = "a,b".splitn(0, ','); | ||
let _ = "a,b".rsplitn(0, ','); | ||
let _ = "a,b".splitn(1, ','); | ||
let _ = [0, 1, 2].splitn(0, |&x| x == 1); | ||
let _ = [0, 1, 2].splitn_mut(0, |&x| x == 1); | ||
let _ = [0, 1, 2].splitn(1, |&x| x == 1); | ||
let _ = [0, 1, 2].rsplitn_mut(1, |&x| x == 1); | ||
|
||
const X: usize = 0; | ||
let _ = "a,b".splitn(X + 1, ','); | ||
let _ = "a,b".splitn(X, ','); | ||
} |
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,75 @@ | ||
error: `splitn` called with `0` splits | ||
--> $DIR/suspicious_splitn.rs:9:13 | ||
| | ||
LL | let _ = "a,b".splitn(0, ','); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::suspicious-splitn` implied by `-D warnings` | ||
= note: the resulting iterator will always return `None` | ||
|
||
error: `rsplitn` called with `0` splits | ||
--> $DIR/suspicious_splitn.rs:10:13 | ||
| | ||
LL | let _ = "a,b".rsplitn(0, ','); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return `None` | ||
|
||
error: `splitn` called with `1` split | ||
--> $DIR/suspicious_splitn.rs:11:13 | ||
| | ||
LL | let _ = "a,b".splitn(1, ','); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return the entire string followed by `None` | ||
|
||
error: `splitn` called with `0` splits | ||
--> $DIR/suspicious_splitn.rs:12:13 | ||
| | ||
LL | let _ = [0, 1, 2].splitn(0, |&x| x == 1); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return `None` | ||
|
||
error: `splitn_mut` called with `0` splits | ||
--> $DIR/suspicious_splitn.rs:13:13 | ||
| | ||
LL | let _ = [0, 1, 2].splitn_mut(0, |&x| x == 1); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return `None` | ||
|
||
error: `splitn` called with `1` split | ||
--> $DIR/suspicious_splitn.rs:14:13 | ||
| | ||
LL | let _ = [0, 1, 2].splitn(1, |&x| x == 1); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return the entire slice followed by `None` | ||
|
||
error: `rsplitn_mut` called with `1` split | ||
--> $DIR/suspicious_splitn.rs:15:13 | ||
| | ||
LL | let _ = [0, 1, 2].rsplitn_mut(1, |&x| x == 1); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return the entire slice followed by `None` | ||
|
||
error: `splitn` called with `1` split | ||
--> $DIR/suspicious_splitn.rs:18:13 | ||
| | ||
LL | let _ = "a,b".splitn(X + 1, ','); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return the entire string followed by `None` | ||
|
||
error: `splitn` called with `0` splits | ||
--> $DIR/suspicious_splitn.rs:19:13 | ||
| | ||
LL | let _ = "a,b".splitn(X, ','); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the resulting iterator will always return `None` | ||
|
||
error: aborting due to 9 previous errors | ||
|