Skip to content

Commit e302dcf

Browse files
committed
add
Signed-off-by: Max Baumann <[email protected]>
1 parent e17b97c commit e302dcf

File tree

8 files changed

+141
-0
lines changed

8 files changed

+141
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,6 +3734,7 @@ Released 2018-09-13
37343734
[`transmute_undefined_repr`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_undefined_repr
37353735
[`transmutes_expressible_as_ptr_casts`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmutes_expressible_as_ptr_casts
37363736
[`transmuting_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmuting_null
3737+
[`trim_split_whitespace`]: https://rust-lang.github.io/rust-clippy/master/index.html#trim_split_whitespace
37373738
[`trivial_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivial_regex
37383739
[`trivially_copy_pass_by_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
37393740
[`try_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#try_err

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ store.register_lints(&[
480480
strings::STRING_SLICE,
481481
strings::STRING_TO_STRING,
482482
strings::STR_TO_STRING,
483+
strings::TRIM_SPLIT_WHITESPACE,
483484
strlen_on_c_strings::STRLEN_ON_C_STRINGS,
484485
suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS,
485486
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,

clippy_lints/src/lib.register_pedantic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
8383
LintId::of(return_self_not_must_use::RETURN_SELF_NOT_MUST_USE),
8484
LintId::of(semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED),
8585
LintId::of(strings::STRING_ADD_ASSIGN),
86+
LintId::of(strings::TRIM_SPLIT_WHITESPACE),
8687
LintId::of(trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS),
8788
LintId::of(trait_bounds::TYPE_REPETITION_IN_BOUNDS),
8889
LintId::of(transmute::TRANSMUTE_PTR_TO_PTR),

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
882882
store.register_early_pass(|| Box::new(pub_use::PubUse));
883883
store.register_late_pass(|| Box::new(format_push_string::FormatPushString));
884884
store.register_late_pass(|| Box::new(bytes_count_to_len::BytesCountToLen));
885+
store.register_late_pass(|| Box::new(strings::TrimSplitWhitespace));
885886
// add lints here, do not remove this comment, it's used in `new_lint`
886887
}
887888

clippy_lints/src/strings.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,50 @@ impl<'tcx> LateLintPass<'tcx> for StringToString {
451451
}
452452
}
453453
}
454+
455+
456+
declare_clippy_lint! {
457+
/// ### What it does
458+
/// Warns about calling `str::trim` (or variants) before `str::split_whitespace`.
459+
///
460+
/// ### Why is this bad?
461+
/// `split_whitespace` already ignores leading and trailing whitespace.
462+
///
463+
/// ### Example
464+
/// ```rust
465+
/// " A B C ".trim().split_whitespace()
466+
/// ```
467+
/// Use instead:
468+
/// ```rust
469+
/// " A B C ".split_whitespace()
470+
/// ```
471+
#[clippy::version = "1.61.0"]
472+
pub TRIM_SPLIT_WHITESPACE,
473+
pedantic,
474+
"using `str::trim()` or alike before `str::split_whitespace`"
475+
}
476+
declare_lint_pass!(TrimSplitWhitespace => [TRIM_SPLIT_WHITESPACE]);
477+
478+
impl<'tcx> LateLintPass<'tcx> for TrimSplitWhitespace {
479+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
480+
if_chain! {
481+
if let ExprKind::MethodCall(path, [recv], split_ws_span) = expr.kind;
482+
if path.ident.name == sym!(split_whitespace);
483+
if let ExprKind::MethodCall(path, [recv], trim_span) = recv.kind;
484+
if let trim_fn_name @ ("trim" | "trim_start" | "trim_end") = path.ident.name.as_str();
485+
let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs();
486+
if recv_ty.is_str() || is_string(cx, recv);
487+
then {
488+
span_lint_and_sugg(
489+
cx,
490+
TRIM_SPLIT_WHITESPACE,
491+
split_ws_span.with_lo(trim_span.lo()),
492+
format!("found call to `str::{}` before `str::split_whitespace`", trim_fn_name).as_str(),
493+
format!("remove `{}()`", trim_fn_name).as_str(),
494+
"split_whitespace()".to_string(),
495+
Applicability::MachineApplicable,
496+
);
497+
}
498+
}
499+
}
500+
}

tests/ui/trim_split_whitespace.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run-rustfix
2+
#![warn(clippy::trim_split_whitespace)]
3+
4+
struct Custom();
5+
impl Custom {
6+
fn trim(self) -> Self {
7+
self
8+
}
9+
fn split_whitespace(self){}
10+
}
11+
12+
fn main() {
13+
// &str
14+
let _ = " A B C ".split_whitespace(); // should trigger lint
15+
let _ = " A B C ".split_whitespace(); // should trigger lint
16+
let _ = " A B C ".split_whitespace(); // should trigger lint
17+
18+
// String
19+
let _ = (" A B C ").to_string().split_whitespace(); // should trigger lint
20+
let _ = (" A B C ").to_string().split_whitespace(); // should trigger lint
21+
let _ = (" A B C ").to_string().split_whitespace(); // should trigger lint
22+
23+
// Custom
24+
let _ = Custom().trim().split_whitespace(); // should not trigger lint
25+
}

tests/ui/trim_split_whitespace.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run-rustfix
2+
#![warn(clippy::trim_split_whitespace)]
3+
4+
struct Custom();
5+
impl Custom {
6+
fn trim(self) -> Self {
7+
self
8+
}
9+
fn split_whitespace(self){}
10+
}
11+
12+
fn main() {
13+
// &str
14+
let _ = " A B C ".trim().split_whitespace(); // should trigger lint
15+
let _ = " A B C ".trim_start().split_whitespace(); // should trigger lint
16+
let _ = " A B C ".trim_end().split_whitespace(); // should trigger lint
17+
18+
// String
19+
let _ = (" A B C ").to_string().trim().split_whitespace(); // should trigger lint
20+
let _ = (" A B C ").to_string().trim_start().split_whitespace(); // should trigger lint
21+
let _ = (" A B C ").to_string().trim_end().split_whitespace(); // should trigger lint
22+
23+
// Custom
24+
let _ = Custom().trim().split_whitespace(); // should not trigger lint
25+
}

tests/ui/trim_split_whitespace.stderr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: found call to `str::trim` before `str::split_whitespace`
2+
--> $DIR/trim_split_whitespace.rs:14:23
3+
|
4+
LL | let _ = " A B C ".trim().split_whitespace(); // should trigger lint
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
6+
|
7+
= note: `-D clippy::trim-split-whitespace` implied by `-D warnings`
8+
9+
error: found call to `str::trim_start` before `str::split_whitespace`
10+
--> $DIR/trim_split_whitespace.rs:15:23
11+
|
12+
LL | let _ = " A B C ".trim_start().split_whitespace(); // should trigger lint
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_start()`: `split_whitespace()`
14+
15+
error: found call to `str::trim_end` before `str::split_whitespace`
16+
--> $DIR/trim_split_whitespace.rs:16:23
17+
|
18+
LL | let _ = " A B C ".trim_end().split_whitespace(); // should trigger lint
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_end()`: `split_whitespace()`
20+
21+
error: found call to `str::trim` before `str::split_whitespace`
22+
--> $DIR/trim_split_whitespace.rs:19:37
23+
|
24+
LL | let _ = (" A B C ").to_string().trim().split_whitespace(); // should trigger lint
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim()`: `split_whitespace()`
26+
27+
error: found call to `str::trim_start` before `str::split_whitespace`
28+
--> $DIR/trim_split_whitespace.rs:20:37
29+
|
30+
LL | let _ = (" A B C ").to_string().trim_start().split_whitespace(); // should trigger lint
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_start()`: `split_whitespace()`
32+
33+
error: found call to `str::trim_end` before `str::split_whitespace`
34+
--> $DIR/trim_split_whitespace.rs:21:37
35+
|
36+
LL | let _ = (" A B C ").to_string().trim_end().split_whitespace(); // should trigger lint
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `trim_end()`: `split_whitespace()`
38+
39+
error: aborting due to 6 previous errors
40+

0 commit comments

Comments
 (0)