|
| 1 | +use rustc_ast::ast::{Expr, ExprKind}; |
| 2 | +use rustc_ast::token::LitKind; |
| 3 | +use rustc_lint::{EarlyContext, EarlyLintPass}; |
| 4 | +use rustc_parse_format::{ParseMode, Parser, Piece}; |
| 5 | +use rustc_session::declare_lint_pass; |
| 6 | +use rustc_span::BytePos; |
| 7 | + |
| 8 | +use clippy_utils::diagnostics::span_lint; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks if string literals have formatting arguments outside of macros |
| 13 | + /// using them (like `format!`). |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// It will likely not generate the expected content. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// let x: Option<usize> = None; |
| 21 | + /// let y = "hello"; |
| 22 | + /// x.expect("{y:?}"); |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```no_run |
| 26 | + /// let x: Option<usize> = None; |
| 27 | + /// let y = "hello"; |
| 28 | + /// x.expect(&format!("{y:?}")); |
| 29 | + /// ``` |
| 30 | + #[clippy::version = "1.83.0"] |
| 31 | + pub LITERAL_STRING_WITH_FORMATTING_ARG, |
| 32 | + suspicious, |
| 33 | + "Checks if string literals have formatting arguments" |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint_pass!(LiteralStringWithFormattingArg => [LITERAL_STRING_WITH_FORMATTING_ARG]); |
| 37 | + |
| 38 | +impl EarlyLintPass for LiteralStringWithFormattingArg { |
| 39 | + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { |
| 40 | + if let ExprKind::Lit(lit) = expr.kind { |
| 41 | + let add = match lit.kind { |
| 42 | + LitKind::Str => 1, |
| 43 | + LitKind::StrRaw(nb) => nb as usize + 2, |
| 44 | + _ => return, |
| 45 | + }; |
| 46 | + let fmt_str = lit.symbol.as_str(); |
| 47 | + let lo = expr.span.lo(); |
| 48 | + let mut current = fmt_str; |
| 49 | + let mut diff_len = 0; |
| 50 | + |
| 51 | + let mut parser = Parser::new(current, None, None, false, ParseMode::Format); |
| 52 | + let mut spans = Vec::new(); |
| 53 | + while let Some(piece) = parser.next() { |
| 54 | + if let Some(error) = parser.errors.last() { |
| 55 | + // We simply ignore the errors and move after them. |
| 56 | + current = ¤t[error.span.end + 1..]; |
| 57 | + diff_len = fmt_str.len() - current.len(); |
| 58 | + parser = Parser::new(current, None, None, false, ParseMode::Format); |
| 59 | + } else if let Piece::NextArgument(arg) = piece { |
| 60 | + let mut pos = arg.position_span; |
| 61 | + pos.start += diff_len; |
| 62 | + pos.end += diff_len; |
| 63 | + |
| 64 | + let start = fmt_str[..pos.start].rfind('{').unwrap_or(pos.start); |
| 65 | + // If this is a unicode character escape, we don't want to lint. |
| 66 | + if start > 1 && fmt_str[start - 2..].starts_with("\\u{") { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + let mut end = fmt_str[pos.end..] |
| 71 | + .find('}') |
| 72 | + .map(|found| found + pos.end) |
| 73 | + .unwrap_or(pos.end); |
| 74 | + if fmt_str[start..end].contains(':') { |
| 75 | + end += 1; |
| 76 | + } |
| 77 | + spans.push( |
| 78 | + expr.span |
| 79 | + .with_hi(lo + BytePos((start + add) as _)) |
| 80 | + .with_lo(lo + BytePos((end + add) as _)), |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + if spans.len() == 1 { |
| 85 | + span_lint( |
| 86 | + cx, |
| 87 | + LITERAL_STRING_WITH_FORMATTING_ARG, |
| 88 | + spans, |
| 89 | + "this is a formatting argument but it is not part of a formatting macro", |
| 90 | + ); |
| 91 | + } else if spans.len() > 1 { |
| 92 | + span_lint( |
| 93 | + cx, |
| 94 | + LITERAL_STRING_WITH_FORMATTING_ARG, |
| 95 | + spans, |
| 96 | + "these are formatting arguments but are not part of a formatting macro", |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments