• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::source::{snippet, snippet_with_applicability};
3 use clippy_utils::ty::is_type_lang_item;
4 use clippy_utils::{get_expr_use_or_unification_node, peel_blocks, SpanlessEq};
5 use clippy_utils::{get_parent_expr, is_lint_allowed, is_path_diagnostic_item, method_calls};
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::def_id::DefId;
9 use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, Node, QPath};
10 use rustc_lint::{LateContext, LateLintPass, LintContext};
11 use rustc_middle::lint::in_external_macro;
12 use rustc_middle::ty;
13 use rustc_session::{declare_lint_pass, declare_tool_lint};
14 use rustc_span::source_map::Spanned;
15 use rustc_span::sym;
16 
17 declare_clippy_lint! {
18     /// ### What it does
19     /// Checks for string appends of the form `x = x + y` (without
20     /// `let`!).
21     ///
22     /// ### Why is this bad?
23     /// It's not really bad, but some people think that the
24     /// `.push_str(_)` method is more readable.
25     ///
26     /// ### Example
27     /// ```rust
28     /// let mut x = "Hello".to_owned();
29     /// x = x + ", World";
30     ///
31     /// // More readable
32     /// x += ", World";
33     /// x.push_str(", World");
34     /// ```
35     #[clippy::version = "pre 1.29.0"]
36     pub STRING_ADD_ASSIGN,
37     pedantic,
38     "using `x = x + ..` where x is a `String` instead of `push_str()`"
39 }
40 
41 declare_clippy_lint! {
42     /// ### What it does
43     /// Checks for all instances of `x + _` where `x` is of type
44     /// `String`, but only if [`string_add_assign`](#string_add_assign) does *not*
45     /// match.
46     ///
47     /// ### Why is this bad?
48     /// It's not bad in and of itself. However, this particular
49     /// `Add` implementation is asymmetric (the other operand need not be `String`,
50     /// but `x` does), while addition as mathematically defined is symmetric, also
51     /// the `String::push_str(_)` function is a perfectly good replacement.
52     /// Therefore, some dislike it and wish not to have it in their code.
53     ///
54     /// That said, other people think that string addition, having a long tradition
55     /// in other languages is actually fine, which is why we decided to make this
56     /// particular lint `allow` by default.
57     ///
58     /// ### Example
59     /// ```rust
60     /// let x = "Hello".to_owned();
61     /// x + ", World";
62     /// ```
63     ///
64     /// Use instead:
65     /// ```rust
66     /// let mut x = "Hello".to_owned();
67     /// x.push_str(", World");
68     /// ```
69     #[clippy::version = "pre 1.29.0"]
70     pub STRING_ADD,
71     restriction,
72     "using `x + ..` where x is a `String` instead of `push_str()`"
73 }
74 
75 declare_clippy_lint! {
76     /// ### What it does
77     /// Checks for the `as_bytes` method called on string literals
78     /// that contain only ASCII characters.
79     ///
80     /// ### Why is this bad?
81     /// Byte string literals (e.g., `b"foo"`) can be used
82     /// instead. They are shorter but less discoverable than `as_bytes()`.
83     ///
84     /// ### Known problems
85     /// `"str".as_bytes()` and the suggested replacement of `b"str"` are not
86     /// equivalent because they have different types. The former is `&[u8]`
87     /// while the latter is `&[u8; 3]`. That means in general they will have a
88     /// different set of methods and different trait implementations.
89     ///
90     /// ```compile_fail
91     /// fn f(v: Vec<u8>) {}
92     ///
93     /// f("...".as_bytes().to_owned()); // works
94     /// f(b"...".to_owned()); // does not work, because arg is [u8; 3] not Vec<u8>
95     ///
96     /// fn g(r: impl std::io::Read) {}
97     ///
98     /// g("...".as_bytes()); // works
99     /// g(b"..."); // does not work
100     /// ```
101     ///
102     /// The actual equivalent of `"str".as_bytes()` with the same type is not
103     /// `b"str"` but `&b"str"[..]`, which is a great deal of punctuation and not
104     /// more readable than a function call.
105     ///
106     /// ### Example
107     /// ```rust
108     /// let bstr = "a byte string".as_bytes();
109     /// ```
110     ///
111     /// Use instead:
112     /// ```rust
113     /// let bstr = b"a byte string";
114     /// ```
115     #[clippy::version = "pre 1.29.0"]
116     pub STRING_LIT_AS_BYTES,
117     nursery,
118     "calling `as_bytes` on a string literal instead of using a byte string literal"
119 }
120 
121 declare_clippy_lint! {
122     /// ### What it does
123     /// Checks for slice operations on strings
124     ///
125     /// ### Why is this bad?
126     /// UTF-8 characters span multiple bytes, and it is easy to inadvertently confuse character
127     /// counts and string indices. This may lead to panics, and should warrant some test cases
128     /// containing wide UTF-8 characters. This lint is most useful in code that should avoid
129     /// panics at all costs.
130     ///
131     /// ### Known problems
132     /// Probably lots of false positives. If an index comes from a known valid position (e.g.
133     /// obtained via `char_indices` over the same string), it is totally OK.
134     ///
135     /// ### Example
136     /// ```rust,should_panic
137     /// &"Ölkanne"[1..];
138     /// ```
139     #[clippy::version = "1.58.0"]
140     pub STRING_SLICE,
141     restriction,
142     "slicing a string"
143 }
144 
145 declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN, STRING_SLICE]);
146 
147 impl<'tcx> LateLintPass<'tcx> for StringAdd {
check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>)148     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
149         if in_external_macro(cx.sess(), e.span) {
150             return;
151         }
152         match e.kind {
153             ExprKind::Binary(
154                 Spanned {
155                     node: BinOpKind::Add, ..
156                 },
157                 left,
158                 _,
159             ) => {
160                 if is_string(cx, left) {
161                     if !is_lint_allowed(cx, STRING_ADD_ASSIGN, e.hir_id) {
162                         let parent = get_parent_expr(cx, e);
163                         if let Some(p) = parent {
164                             if let ExprKind::Assign(target, _, _) = p.kind {
165                                 // avoid duplicate matches
166                                 if SpanlessEq::new(cx).eq_expr(target, left) {
167                                     return;
168                                 }
169                             }
170                         }
171                     }
172                     span_lint(
173                         cx,
174                         STRING_ADD,
175                         e.span,
176                         "you added something to a string. Consider using `String::push_str()` instead",
177                     );
178                 }
179             },
180             ExprKind::Assign(target, src, _) => {
181                 if is_string(cx, target) && is_add(cx, src, target) {
182                     span_lint(
183                         cx,
184                         STRING_ADD_ASSIGN,
185                         e.span,
186                         "you assigned the result of adding something to this string. Consider using \
187                          `String::push_str()` instead",
188                     );
189                 }
190             },
191             ExprKind::Index(target, _idx) => {
192                 let e_ty = cx.typeck_results().expr_ty(target).peel_refs();
193                 if e_ty.is_str() || is_type_lang_item(cx, e_ty, LangItem::String) {
194                     span_lint(
195                         cx,
196                         STRING_SLICE,
197                         e.span,
198                         "indexing into a string may panic if the index is within a UTF-8 character",
199                     );
200                 }
201             },
202             _ => {},
203         }
204     }
205 }
206 
is_string(cx: &LateContext<'_>, e: &Expr<'_>) -> bool207 fn is_string(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
208     is_type_lang_item(cx, cx.typeck_results().expr_ty(e).peel_refs(), LangItem::String)
209 }
210 
is_add(cx: &LateContext<'_>, src: &Expr<'_>, target: &Expr<'_>) -> bool211 fn is_add(cx: &LateContext<'_>, src: &Expr<'_>, target: &Expr<'_>) -> bool {
212     match peel_blocks(src).kind {
213         ExprKind::Binary(
214             Spanned {
215                 node: BinOpKind::Add, ..
216             },
217             left,
218             _,
219         ) => SpanlessEq::new(cx).eq_expr(target, left),
220         _ => false,
221     }
222 }
223 
224 declare_clippy_lint! {
225     /// ### What it does
226     /// Check if the string is transformed to byte array and casted back to string.
227     ///
228     /// ### Why is this bad?
229     /// It's unnecessary, the string can be used directly.
230     ///
231     /// ### Example
232     /// ```rust
233     /// std::str::from_utf8(&"Hello World!".as_bytes()[6..11]).unwrap();
234     /// ```
235     ///
236     /// Use instead:
237     /// ```rust
238     /// &"Hello World!"[6..11];
239     /// ```
240     #[clippy::version = "1.50.0"]
241     pub STRING_FROM_UTF8_AS_BYTES,
242     complexity,
243     "casting string slices to byte slices and back"
244 }
245 
246 // Max length a b"foo" string can take
247 const MAX_LENGTH_BYTE_STRING_LIT: usize = 32;
248 
249 declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES, STRING_FROM_UTF8_AS_BYTES]);
250 
251 impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
252     #[expect(clippy::too_many_lines)]
check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>)253     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
254         use rustc_ast::LitKind;
255 
256         if_chain! {
257             // Find std::str::converts::from_utf8
258             if let ExprKind::Call(fun, args) = e.kind;
259             if is_path_diagnostic_item(cx, fun, sym::str_from_utf8);
260 
261             // Find string::as_bytes
262             if let ExprKind::AddrOf(BorrowKind::Ref, _, args) = args[0].kind;
263             if let ExprKind::Index(left, right) = args.kind;
264             let (method_names, expressions, _) = method_calls(left, 1);
265             if method_names.len() == 1;
266             if expressions.len() == 1;
267             if expressions[0].1.is_empty();
268             if method_names[0] == sym!(as_bytes);
269 
270             // Check for slicer
271             if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), _, _) = right.kind;
272 
273             then {
274                 let mut applicability = Applicability::MachineApplicable;
275                 let string_expression = &expressions[0].0;
276 
277                 let snippet_app = snippet_with_applicability(
278                     cx,
279                     string_expression.span, "..",
280                     &mut applicability,
281                 );
282 
283                 span_lint_and_sugg(
284                     cx,
285                     STRING_FROM_UTF8_AS_BYTES,
286                     e.span,
287                     "calling a slice of `as_bytes()` with `from_utf8` should be not necessary",
288                     "try",
289                     format!("Some(&{snippet_app}[{}])", snippet(cx, right.span, "..")),
290                     applicability
291                 )
292             }
293         }
294 
295         if_chain! {
296             if !in_external_macro(cx.sess(), e.span);
297             if let ExprKind::MethodCall(path, receiver, ..) = &e.kind;
298             if path.ident.name == sym!(as_bytes);
299             if let ExprKind::Lit(lit) = &receiver.kind;
300             if let LitKind::Str(lit_content, _) = &lit.node;
301             then {
302                 let callsite = snippet(cx, receiver.span.source_callsite(), r#""foo""#);
303                 let mut applicability = Applicability::MachineApplicable;
304                 if callsite.starts_with("include_str!") {
305                     span_lint_and_sugg(
306                         cx,
307                         STRING_LIT_AS_BYTES,
308                         e.span,
309                         "calling `as_bytes()` on `include_str!(..)`",
310                         "consider using `include_bytes!(..)` instead",
311                         snippet_with_applicability(cx, receiver.span, r#""foo""#, &mut applicability).replacen(
312                             "include_str",
313                             "include_bytes",
314                             1,
315                         ),
316                         applicability,
317                     );
318                 } else if lit_content.as_str().is_ascii()
319                     && lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT
320                     && !receiver.span.from_expansion()
321                 {
322                     if let Some((parent, id)) = get_expr_use_or_unification_node(cx.tcx, e)
323                         && let Node::Expr(parent) = parent
324                         && let ExprKind::Match(scrutinee, ..) = parent.kind
325                         && scrutinee.hir_id == id
326                     {
327                         // Don't lint. Byte strings produce `&[u8; N]` whereas `as_bytes()` produces
328                         // `&[u8]`. This change would prevent matching with different sized slices.
329                     } else {
330                         span_lint_and_sugg(
331                             cx,
332                             STRING_LIT_AS_BYTES,
333                             e.span,
334                             "calling `as_bytes()` on a string literal",
335                             "consider using a byte string literal instead",
336                             format!(
337                                 "b{}",
338                                 snippet_with_applicability(cx, receiver.span, r#""foo""#, &mut applicability)
339                             ),
340                             applicability,
341                         );
342                     }
343                 }
344             }
345         }
346 
347         if_chain! {
348             if let ExprKind::MethodCall(path, recv, [], _) = &e.kind;
349             if path.ident.name == sym!(into_bytes);
350             if let ExprKind::MethodCall(path, recv, [], _) = &recv.kind;
351             if matches!(path.ident.name.as_str(), "to_owned" | "to_string");
352             if let ExprKind::Lit(lit) = &recv.kind;
353             if let LitKind::Str(lit_content, _) = &lit.node;
354 
355             if lit_content.as_str().is_ascii();
356             if lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT;
357             if !recv.span.from_expansion();
358             then {
359                 let mut applicability = Applicability::MachineApplicable;
360 
361                 span_lint_and_sugg(
362                     cx,
363                     STRING_LIT_AS_BYTES,
364                     e.span,
365                     "calling `into_bytes()` on a string literal",
366                     "consider using a byte string literal instead",
367                     format!(
368                         "b{}.to_vec()",
369                         snippet_with_applicability(cx, recv.span, r#""..""#, &mut applicability)
370                     ),
371                     applicability,
372                 );
373             }
374         }
375     }
376 }
377 
378 declare_clippy_lint! {
379     /// ### What it does
380     /// This lint checks for `.to_string()` method calls on values of type `&str`.
381     ///
382     /// ### Why is this bad?
383     /// The `to_string` method is also used on other types to convert them to a string.
384     /// When called on a `&str` it turns the `&str` into the owned variant `String`, which can be better
385     /// expressed with `.to_owned()`.
386     ///
387     /// ### Example
388     /// ```rust
389     /// // example code where clippy issues a warning
390     /// let _ = "str".to_string();
391     /// ```
392     /// Use instead:
393     /// ```rust
394     /// // example code which does not raise clippy warning
395     /// let _ = "str".to_owned();
396     /// ```
397     #[clippy::version = "pre 1.29.0"]
398     pub STR_TO_STRING,
399     restriction,
400     "using `to_string()` on a `&str`, which should be `to_owned()`"
401 }
402 
403 declare_lint_pass!(StrToString => [STR_TO_STRING]);
404 
405 impl<'tcx> LateLintPass<'tcx> for StrToString {
check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>)406     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
407         if_chain! {
408             if let ExprKind::MethodCall(path, self_arg, ..) = &expr.kind;
409             if path.ident.name == sym::to_string;
410             let ty = cx.typeck_results().expr_ty(self_arg);
411             if let ty::Ref(_, ty, ..) = ty.kind();
412             if ty.is_str();
413             then {
414                 span_lint_and_help(
415                     cx,
416                     STR_TO_STRING,
417                     expr.span,
418                     "`to_string()` called on a `&str`",
419                     None,
420                     "consider using `.to_owned()`",
421                 );
422             }
423         }
424     }
425 }
426 
427 declare_clippy_lint! {
428     /// ### What it does
429     /// This lint checks for `.to_string()` method calls on values of type `String`.
430     ///
431     /// ### Why is this bad?
432     /// The `to_string` method is also used on other types to convert them to a string.
433     /// When called on a `String` it only clones the `String`, which can be better expressed with `.clone()`.
434     ///
435     /// ### Example
436     /// ```rust
437     /// // example code where clippy issues a warning
438     /// let msg = String::from("Hello World");
439     /// let _ = msg.to_string();
440     /// ```
441     /// Use instead:
442     /// ```rust
443     /// // example code which does not raise clippy warning
444     /// let msg = String::from("Hello World");
445     /// let _ = msg.clone();
446     /// ```
447     #[clippy::version = "pre 1.29.0"]
448     pub STRING_TO_STRING,
449     restriction,
450     "using `to_string()` on a `String`, which should be `clone()`"
451 }
452 
453 declare_lint_pass!(StringToString => [STRING_TO_STRING]);
454 
455 impl<'tcx> LateLintPass<'tcx> for StringToString {
check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>)456     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
457         if_chain! {
458             if let ExprKind::MethodCall(path, self_arg, ..) = &expr.kind;
459             if path.ident.name == sym::to_string;
460             let ty = cx.typeck_results().expr_ty(self_arg);
461             if is_type_lang_item(cx, ty, LangItem::String);
462             then {
463                 span_lint_and_help(
464                     cx,
465                     STRING_TO_STRING,
466                     expr.span,
467                     "`to_string()` called on a `String`",
468                     None,
469                     "consider using `.clone()`",
470                 );
471             }
472         }
473     }
474 }
475 
476 declare_clippy_lint! {
477     /// ### What it does
478     /// Warns about calling `str::trim` (or variants) before `str::split_whitespace`.
479     ///
480     /// ### Why is this bad?
481     /// `split_whitespace` already ignores leading and trailing whitespace.
482     ///
483     /// ### Example
484     /// ```rust
485     /// " A B C ".trim().split_whitespace();
486     /// ```
487     /// Use instead:
488     /// ```rust
489     /// " A B C ".split_whitespace();
490     /// ```
491     #[clippy::version = "1.62.0"]
492     pub TRIM_SPLIT_WHITESPACE,
493     style,
494     "using `str::trim()` or alike before `str::split_whitespace`"
495 }
496 declare_lint_pass!(TrimSplitWhitespace => [TRIM_SPLIT_WHITESPACE]);
497 
498 impl<'tcx> LateLintPass<'tcx> for TrimSplitWhitespace {
check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>)499     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
500         let tyckres = cx.typeck_results();
501         if_chain! {
502             if let ExprKind::MethodCall(path, split_recv, [], split_ws_span) = expr.kind;
503             if path.ident.name == sym!(split_whitespace);
504             if let Some(split_ws_def_id) = tyckres.type_dependent_def_id(expr.hir_id);
505             if cx.tcx.is_diagnostic_item(sym::str_split_whitespace, split_ws_def_id);
506             if let ExprKind::MethodCall(path, _trim_recv, [], trim_span) = split_recv.kind;
507             if let trim_fn_name @ ("trim" | "trim_start" | "trim_end") = path.ident.name.as_str();
508             if let Some(trim_def_id) = tyckres.type_dependent_def_id(split_recv.hir_id);
509             if is_one_of_trim_diagnostic_items(cx, trim_def_id);
510             then {
511                 span_lint_and_sugg(
512                     cx,
513                     TRIM_SPLIT_WHITESPACE,
514                     trim_span.with_hi(split_ws_span.lo()),
515                     &format!("found call to `str::{trim_fn_name}` before `str::split_whitespace`"),
516                     &format!("remove `{trim_fn_name}()`"),
517                     String::new(),
518                     Applicability::MachineApplicable,
519                 );
520             }
521         }
522     }
523 }
524 
is_one_of_trim_diagnostic_items(cx: &LateContext<'_>, trim_def_id: DefId) -> bool525 fn is_one_of_trim_diagnostic_items(cx: &LateContext<'_>, trim_def_id: DefId) -> bool {
526     cx.tcx.is_diagnostic_item(sym::str_trim, trim_def_id)
527         || cx.tcx.is_diagnostic_item(sym::str_trim_start, trim_def_id)
528         || cx.tcx.is_diagnostic_item(sym::str_trim_end, trim_def_id)
529 }
530