|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; |
| 2 | +use rustc_ast::{ |
| 3 | + ast::{Expr, ExprKind}, |
| 4 | + token::LitKind, |
| 5 | +}; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; |
| 8 | +use rustc_middle::lint::in_external_macro; |
| 9 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for raw string literals where a string literal can be used instead. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// It's just unnecessary. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```rust |
| 20 | + /// let r = r"Hello, world!"; |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust |
| 24 | + /// let r = "Hello, world!"; |
| 25 | + /// ``` |
| 26 | + #[clippy::version = "1.72.0"] |
| 27 | + pub NEEDLESS_RAW_STRING, |
| 28 | + complexity, |
| 29 | + "suggests using a string literal when a raw string literal is unnecessary" |
| 30 | +} |
| 31 | +declare_clippy_lint! { |
| 32 | + /// ### What it does |
| 33 | + /// Checks for raw string literals with an unnecessary amount of hashes around them. |
| 34 | + /// |
| 35 | + /// ### Why is this bad? |
| 36 | + /// It's just unnecessary, and makes it look like there's more escaping needed than is actually |
| 37 | + /// necessary. |
| 38 | + /// |
| 39 | + /// ### Example |
| 40 | + /// ```rust |
| 41 | + /// let r = r###"Hello, "world"!"###; |
| 42 | + /// ``` |
| 43 | + /// Use instead: |
| 44 | + /// ```rust |
| 45 | + /// let r = r#"Hello, "world"!"#; |
| 46 | + /// ``` |
| 47 | + #[clippy::version = "1.72.0"] |
| 48 | + pub NEEDLESS_RAW_STRING_HASHES, |
| 49 | + complexity, |
| 50 | + "suggests reducing the number of hashes around a raw string literal" |
| 51 | +} |
| 52 | +impl_lint_pass!(RawStrings => [NEEDLESS_RAW_STRING, NEEDLESS_RAW_STRING_HASHES]); |
| 53 | + |
| 54 | +pub struct RawStrings { |
| 55 | + pub needless_raw_string_hashes_allow_one: bool, |
| 56 | +} |
| 57 | + |
| 58 | +impl EarlyLintPass for RawStrings { |
| 59 | + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { |
| 60 | + if !in_external_macro(cx.sess(), expr.span) |
| 61 | + && let ExprKind::Lit(lit) = expr.kind |
| 62 | + && let LitKind::StrRaw(num) | LitKind::ByteStrRaw(num) | LitKind::CStrRaw(num) = lit.kind |
| 63 | + { |
| 64 | + let prefix = match lit.kind { |
| 65 | + LitKind::StrRaw(..) => "r", |
| 66 | + LitKind::ByteStrRaw(..) => "br", |
| 67 | + LitKind::CStrRaw(..) => "cr", |
| 68 | + _ => unreachable!(), |
| 69 | + }; |
| 70 | + if !snippet(cx, expr.span, prefix).trim().starts_with(prefix) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + #[allow(clippy::cast_possible_truncation)] |
| 75 | + let req = lit.symbol.as_str().as_bytes() |
| 76 | + .split(|&b| b == b'"') |
| 77 | + .skip(1) |
| 78 | + .map(|bs| 1 + bs.iter().take_while(|&&b| b == b'#').count() as u8) |
| 79 | + .max() |
| 80 | + .unwrap_or(0); |
| 81 | + |
| 82 | + if req < num { |
| 83 | + let hashes = "#".repeat(req as usize); |
| 84 | + |
| 85 | + span_lint_and_sugg( |
| 86 | + cx, |
| 87 | + NEEDLESS_RAW_STRING_HASHES, |
| 88 | + expr.span, |
| 89 | + "unnecessary hashes around raw string literal", |
| 90 | + "try", |
| 91 | + format!(r#"{prefix}{hashes}"{}"{hashes}"#, lit.symbol), |
| 92 | + Applicability::MachineApplicable, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + if !lit.symbol.as_str().contains(['\\', '"']) { |
| 97 | + span_lint_and_sugg( |
| 98 | + cx, |
| 99 | + NEEDLESS_RAW_STRING, |
| 100 | + expr.span, |
| 101 | + "unnecessary raw string literal", |
| 102 | + "try", |
| 103 | + format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol), |
| 104 | + Applicability::MachineApplicable, |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments