Skip to content

Commit

Permalink
8733: Make docs more precise and add description of known problems
Browse files Browse the repository at this point in the history
  • Loading branch information
torfsen committed Dec 27, 2023
1 parent 37a0c79 commit a0ef520
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 8 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/methods/str_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a0ef520

Please sign in to comment.