-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #11987 - torfsen:8733-Suggest-str.lines-when-splitting-…
…at-newlines, r=Jarcho Issue 8733: Suggest `str.lines` when splitting at hard-coded newlines Fixes #8733. ``` changelog: [`splitting_strings_at_newlines`]: New lint that suggests `str.lines` over splitting at hard-coded newlines ``` This is my first PR to Clippy and one of my first Rust PRs in general -- please feel free to nitpick, I'm thankful for any opportunity to learn! I'd be especially interested in feedback to the following points: * Is checking for `'\n'`, `"\n"`, and `"\r\n"` as arguments to `split` enough, or should we do more (e.g. checking for constants that have those values if that is possible)? * Could the code be written in a more idiomatic way? * Is the default `".."` for `snippet` a good choice? I copied it from other uses of `snippet` in the code base, but I'm not entirely sure. * Is the category `suspicious` a good choice? * Is the suggestion applicability `MaybeIncorrect` a good choice? I used it because the return type of `lines` is not exactly the same as that of `split`.
- Loading branch information
Showing
7 changed files
with
430 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
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,38 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::visitors::is_const_evaluatable; | ||
use rustc_ast::ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::STR_SPLIT_AT_NEWLINE; | ||
|
||
pub(super) fn check<'a>(cx: &LateContext<'a>, expr: &'_ Expr<'_>, split_recv: &'a Expr<'_>, split_arg: &'_ Expr<'_>) { | ||
// We're looking for `A.trim().split(B)`, where the adjusted type of `A` is `&str` (e.g. an | ||
// expression returning `String`), and `B` is a `Pattern` that hard-codes a newline (either `"\n"` | ||
// or `"\r\n"`). There are a lot of ways to specify a pattern, and this lint only checks the most | ||
// basic ones: a `'\n'`, `"\n"`, and `"\r\n"`. | ||
if let ExprKind::MethodCall(trim_method_name, trim_recv, [], _) = split_recv.kind | ||
&& trim_method_name.ident.as_str() == "trim" | ||
&& cx.typeck_results().expr_ty_adjusted(trim_recv).peel_refs().is_str() | ||
&& !is_const_evaluatable(cx, trim_recv) | ||
&& let ExprKind::Lit(split_lit) = split_arg.kind | ||
&& (matches!(split_lit.node, LitKind::Char('\n')) | ||
|| matches!(split_lit.node, LitKind::Str(sym, _) if (sym.as_str() == "\n" || sym.as_str() == "\r\n"))) | ||
{ | ||
let mut app = Applicability::MaybeIncorrect; | ||
span_lint_and_sugg( | ||
cx, | ||
STR_SPLIT_AT_NEWLINE, | ||
expr.span, | ||
"using `str.trim().split()` with hard-coded newlines", | ||
"use `str.lines()` instead", | ||
format!( | ||
"{}.lines()", | ||
snippet_with_context(cx, trim_recv.span, expr.span.ctxt(), "..", &mut app).0 | ||
), | ||
app, | ||
); | ||
} | ||
} |
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,145 @@ | ||
#![warn(clippy::str_split_at_newline)] | ||
|
||
use core::str::Split; | ||
use std::ops::Deref; | ||
|
||
struct NotStr<'a> { | ||
s: &'a str, | ||
} | ||
|
||
impl<'a> NotStr<'a> { | ||
fn trim(&'a self) -> &'a str { | ||
self.s | ||
} | ||
} | ||
|
||
struct DerefsIntoNotStr<'a> { | ||
not_str: &'a NotStr<'a>, | ||
} | ||
|
||
impl<'a> Deref for DerefsIntoNotStr<'a> { | ||
type Target = NotStr<'a>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.not_str | ||
} | ||
} | ||
|
||
struct DerefsIntoStr<'a> { | ||
s: &'a str, | ||
} | ||
|
||
impl<'a> Deref for DerefsIntoStr<'a> { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.s | ||
} | ||
} | ||
|
||
macro_rules! trim_split { | ||
( $x:expr, $y:expr ) => { | ||
$x.trim().split($y); | ||
}; | ||
} | ||
|
||
macro_rules! make_str { | ||
( $x: expr ) => { | ||
format!("x={}", $x) | ||
}; | ||
} | ||
|
||
fn main() { | ||
let s1 = "hello\nworld\n"; | ||
let s2 = s1.to_owned(); | ||
|
||
// CASES THAT SHOULD EMIT A LINT | ||
|
||
// Splitting a `str` variable at "\n" or "\r\n" after trimming should warn | ||
let _ = s1.lines(); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s1.lines(); | ||
let _ = s1.lines(); | ||
|
||
// Splitting a `String` variable at "\n" or "\r\n" after trimming should warn | ||
let _ = s2.lines(); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s2.lines(); | ||
let _ = s2.lines(); | ||
|
||
// Splitting a variable that derefs into `str` at "\n" or "\r\n" after trimming should warn. | ||
let s3 = DerefsIntoStr { s: s1 }; | ||
let _ = s3.lines(); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s3.lines(); | ||
let _ = s3.lines(); | ||
|
||
// If the `&str` is generated by a macro then the macro should not be expanded in the suggested fix. | ||
let _ = make_str!(s1).lines(); | ||
|
||
// CASES THAT SHOULD NOT EMIT A LINT | ||
|
||
// Splitting a `str` constant at "\n" or "\r\n" after trimming should not warn | ||
let _ = "hello\nworld\n".trim().split('\n'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = "hello\nworld\n".trim().split("\n"); | ||
let _ = "hello\nworld\n".trim().split("\r\n"); | ||
|
||
// Splitting a `str` variable at "\n" or "\r\n" without trimming should not warn, since it is not | ||
// equivalent | ||
let _ = s1.split('\n'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s1.split("\n"); | ||
let _ = s1.split("\r\n"); | ||
|
||
// Splitting a `String` variable at "\n" or "\r\n" without trimming should not warn. | ||
let _ = s2.split('\n'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s2.split("\n"); | ||
|
||
// Splitting a variable that derefs into `str` at "\n" or "\r\n" without trimming should not warn. | ||
let _ = s3.split('\n'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s3.split("\n"); | ||
let _ = s3.split("\r\n"); | ||
let _ = s2.split("\r\n"); | ||
|
||
// Splitting a `str` variable at other separators should not warn | ||
let _ = s1.trim().split('\r'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s1.trim().split("\r"); | ||
let _ = s1.trim().split("\n\r"); | ||
let _ = s1.trim().split("\r \n"); | ||
|
||
// Splitting a `String` variable at other separators should not warn | ||
let _ = s2.trim().split('\r'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s2.trim().split("\r"); | ||
let _ = s2.trim().split("\n\r"); | ||
|
||
// Splitting a variable that derefs into `str` at other separators should not warn | ||
let _ = s3.trim().split('\r'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = s3.trim().split("\r"); | ||
let _ = s3.trim().split("\n\r"); | ||
let _ = s3.trim().split("\r \n"); | ||
let _ = s2.trim().split("\r \n"); | ||
|
||
// Using `trim` and `split` on other types should not warn | ||
let not_str = NotStr { s: s1 }; | ||
let _ = not_str.trim().split('\n'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = not_str.trim().split("\n"); | ||
let _ = not_str.trim().split("\r\n"); | ||
let derefs_into_not_str = DerefsIntoNotStr { not_str: ¬_str }; | ||
let _ = derefs_into_not_str.trim().split('\n'); | ||
#[allow(clippy::single_char_pattern)] | ||
let _ = derefs_into_not_str.trim().split("\n"); | ||
let _ = derefs_into_not_str.trim().split("\r\n"); | ||
|
||
// Code generated by macros should not create a warning | ||
trim_split!(s1, "\r\n"); | ||
trim_split!("hello\nworld\n", "\r\n"); | ||
trim_split!(s2, "\r\n"); | ||
trim_split!(s3, "\r\n"); | ||
} |
Oops, something went wrong.