diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 1da2b27e843a..fcb4d2128be8 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3837,7 +3837,7 @@ declare_clippy_lint! { /// /// ### Why is this bad? /// - /// Hard-coding the line endings makes the code less portable. `str.lines` should be used instead. + /// Hard-coding the line endings makes the code less compatible. `str.lines` should be used instead. /// /// ### Example /// ```no_run @@ -3847,6 +3847,13 @@ declare_clippy_lint! { /// ```no_run /// "some\ntext\nwith\nnewlines\n".lines(); /// ``` + /// + /// ### Known Problems + /// + /// This lint cannot detect if the split is intentionally restricted to a single type of newline (`"\n"` or + /// `"\r\n"`), for example during the parsing of a specific file format in which precisely one newline type is + /// valid. + /// ``` #[clippy::version = "1.76.0"] pub STR_SPLIT_AT_NEWLINE, pedantic, diff --git a/clippy_lints/src/methods/str_split.rs b/clippy_lints/src/methods/str_split.rs index 43fda629480e..3586e11f56ab 100644 --- a/clippy_lints/src/methods/str_split.rs +++ b/clippy_lints/src/methods/str_split.rs @@ -9,11 +9,10 @@ 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. a `str` - // literal or 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"`. - + // 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()