|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::macros::macro_backtrace; |
| 3 | +use clippy_utils::source::snippet_opt; |
| 4 | +use clippy_utils::{match_def_path, tokenize_with_text}; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{Expr, ExprKind}; |
| 8 | +use rustc_lexer::TokenKind; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks that the `concat!` macro has at least two arguments. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// If there are less than 2 arguments, then calling the macro is doing nothing. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```no_run |
| 21 | + /// let x = concat!("a"); |
| 22 | + /// ``` |
| 23 | + /// Use instead: |
| 24 | + /// ```no_run |
| 25 | + /// let x = "a"; |
| 26 | + /// ``` |
| 27 | + #[clippy::version = "1.85.0"] |
| 28 | + pub USELESS_CONCAT, |
| 29 | + complexity, |
| 30 | + "checks that the `concat` macro has at least two arguments" |
| 31 | +} |
| 32 | + |
| 33 | +declare_lint_pass!(UselessConcat => [USELESS_CONCAT]); |
| 34 | + |
| 35 | +impl LateLintPass<'_> for UselessConcat { |
| 36 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 37 | + // Check that the expression is generated by a macro. |
| 38 | + if expr.span.from_expansion() |
| 39 | + // Check that it's a string literal. |
| 40 | + && let ExprKind::Lit(lit) = expr.kind |
| 41 | + && let LitKind::Str(_, _) = lit.node |
| 42 | + // Get the direct parent of the expression. |
| 43 | + && let Some(macro_call) = macro_backtrace(expr.span).next() |
| 44 | + // Check if the `concat` macro from the `core` library. |
| 45 | + && match_def_path(cx, macro_call.def_id, &["core", "macros", "builtin", "concat"]) |
| 46 | + // We get the original code to parse it. |
| 47 | + && let Some(original_code) = snippet_opt(cx, macro_call.span) |
| 48 | + // This check allows us to ensure that the code snippet: |
| 49 | + // 1. Doesn't come from proc-macro expansion. |
| 50 | + // 2. Doesn't come from foreign macro expansion. |
| 51 | + // |
| 52 | + // It works as follows: if the snippet we get doesn't contain `concat!(`, then it |
| 53 | + // means it's not code written in the current crate so we shouldn't lint. |
| 54 | + && let mut parts = original_code.split('!') |
| 55 | + && parts.next().is_some_and(|p| p.trim() == "concat") |
| 56 | + && parts.next().is_some_and(|p| p.trim().starts_with('(')) |
| 57 | + { |
| 58 | + let mut literal = None; |
| 59 | + let mut nb_commas = 0; |
| 60 | + let mut nb_idents = 0; |
| 61 | + for (token_kind, token_s, _) in tokenize_with_text(&original_code) { |
| 62 | + match token_kind { |
| 63 | + TokenKind::Eof => break, |
| 64 | + TokenKind::Literal { .. } => { |
| 65 | + if literal.is_some() { |
| 66 | + return; |
| 67 | + } |
| 68 | + literal = Some(token_s); |
| 69 | + }, |
| 70 | + TokenKind::Ident => nb_idents += 1, |
| 71 | + TokenKind::Comma => { |
| 72 | + nb_commas += 1; |
| 73 | + if nb_commas > 1 { |
| 74 | + return; |
| 75 | + } |
| 76 | + }, |
| 77 | + // We're inside a macro definition and we are manipulating something we likely |
| 78 | + // shouldn't, so aborting. |
| 79 | + TokenKind::Dollar => return, |
| 80 | + _ => {}, |
| 81 | + } |
| 82 | + } |
| 83 | + let literal = match literal { |
| 84 | + Some(lit) => { |
| 85 | + // Literals can also be number, so we need to check this case too. |
| 86 | + if lit.starts_with('"') { |
| 87 | + lit.to_string() |
| 88 | + } else { |
| 89 | + format!("\"{lit}\"") |
| 90 | + } |
| 91 | + }, |
| 92 | + None => "\"\"".to_string(), |
| 93 | + }; |
| 94 | + // There should always be the ident of the `concat` macro. |
| 95 | + if nb_idents == 1 { |
| 96 | + span_lint_and_sugg( |
| 97 | + cx, |
| 98 | + USELESS_CONCAT, |
| 99 | + macro_call.span, |
| 100 | + "unneeded use of `concat!` macro", |
| 101 | + "replace with", |
| 102 | + literal, |
| 103 | + Applicability::MachineApplicable, |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments