|
| 1 | +use std::{iter::once, ops::ControlFlow}; |
| 2 | + |
| 3 | +use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; |
| 4 | +use rustc_ast::{ |
| 5 | + ast::{Expr, ExprKind}, |
| 6 | + token::LitKind, |
| 7 | +}; |
| 8 | +use rustc_errors::Applicability; |
| 9 | +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; |
| 10 | +use rustc_middle::lint::in_external_macro; |
| 11 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for raw string literals where a string literal can be used instead. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// It's just unnecessary, but there are many cases where using a raw string literal is more |
| 19 | + /// idiomatic than a string literal, so it's opt-in. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust |
| 23 | + /// let r = r"Hello, world!"; |
| 24 | + /// ``` |
| 25 | + /// Use instead: |
| 26 | + /// ```rust |
| 27 | + /// let r = "Hello, world!"; |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.72.0"] |
| 30 | + pub NEEDLESS_RAW_STRINGS, |
| 31 | + restriction, |
| 32 | + "suggests using a string literal when a raw string literal is unnecessary" |
| 33 | +} |
| 34 | +declare_clippy_lint! { |
| 35 | + /// ### What it does |
| 36 | + /// Checks for raw string literals with an unnecessary amount of hashes around them. |
| 37 | + /// |
| 38 | + /// ### Why is this bad? |
| 39 | + /// It's just unnecessary, and makes it look like there's more escaping needed than is actually |
| 40 | + /// necessary. |
| 41 | + /// |
| 42 | + /// ### Example |
| 43 | + /// ```rust |
| 44 | + /// let r = r###"Hello, "world"!"###; |
| 45 | + /// ``` |
| 46 | + /// Use instead: |
| 47 | + /// ```rust |
| 48 | + /// let r = r#"Hello, "world"!"#; |
| 49 | + /// ``` |
| 50 | + #[clippy::version = "1.72.0"] |
| 51 | + pub NEEDLESS_RAW_STRING_HASHES, |
| 52 | + style, |
| 53 | + "suggests reducing the number of hashes around a raw string literal" |
| 54 | +} |
| 55 | +impl_lint_pass!(RawStrings => [NEEDLESS_RAW_STRINGS, NEEDLESS_RAW_STRING_HASHES]); |
| 56 | + |
| 57 | +pub struct RawStrings { |
| 58 | + pub needless_raw_string_hashes_allow_one: bool, |
| 59 | +} |
| 60 | + |
| 61 | +impl EarlyLintPass for RawStrings { |
| 62 | + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { |
| 63 | + if !in_external_macro(cx.sess(), expr.span) |
| 64 | + && let ExprKind::Lit(lit) = expr.kind |
| 65 | + && let LitKind::StrRaw(max) | LitKind::ByteStrRaw(max) | LitKind::CStrRaw(max) = lit.kind |
| 66 | + { |
| 67 | + let str = lit.symbol.as_str(); |
| 68 | + let prefix = match lit.kind { |
| 69 | + LitKind::StrRaw(..) => "r", |
| 70 | + LitKind::ByteStrRaw(..) => "br", |
| 71 | + LitKind::CStrRaw(..) => "cr", |
| 72 | + _ => unreachable!(), |
| 73 | + }; |
| 74 | + if !snippet(cx, expr.span, prefix).trim().starts_with(prefix) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if !str.contains(['\\', '"']) { |
| 79 | + span_lint_and_sugg( |
| 80 | + cx, |
| 81 | + NEEDLESS_RAW_STRINGS, |
| 82 | + expr.span, |
| 83 | + "unnecessary raw string literal", |
| 84 | + "try", |
| 85 | + format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol), |
| 86 | + Applicability::MachineApplicable, |
| 87 | + ); |
| 88 | + |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + let req = { |
| 93 | + let mut following_quote = false; |
| 94 | + let mut req = 0; |
| 95 | + // `once` so a raw string ending in hashes is still checked |
| 96 | + let num = str.as_bytes().iter().chain(once(&0)).try_fold(0u8, |acc, &b| { |
| 97 | + match b { |
| 98 | + b'"' => (following_quote, req) = (true, 1), |
| 99 | + // I'm a bit surprised the compiler didn't optimize this out, there's no |
| 100 | + // branch but it still ends up doing an unnecessary comparison, it's: |
| 101 | + // - cmp r9b,1h |
| 102 | + // - sbb cl,-1h |
| 103 | + // which will add 1 if it's true. With this change, it becomes: |
| 104 | + // - add cl,r9b |
| 105 | + // isn't that so much nicer? |
| 106 | + b'#' => req += u8::from(following_quote), |
| 107 | + _ => { |
| 108 | + if following_quote { |
| 109 | + following_quote = false; |
| 110 | + |
| 111 | + if req == max { |
| 112 | + return ControlFlow::Break(req); |
| 113 | + } |
| 114 | + |
| 115 | + return ControlFlow::Continue(acc.max(req)); |
| 116 | + } |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + ControlFlow::Continue(acc) |
| 121 | + }); |
| 122 | + |
| 123 | + match num { |
| 124 | + ControlFlow::Continue(num) | ControlFlow::Break(num) => num, |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + if req < max { |
| 129 | + let hashes = "#".repeat(req as usize); |
| 130 | + |
| 131 | + span_lint_and_sugg( |
| 132 | + cx, |
| 133 | + NEEDLESS_RAW_STRING_HASHES, |
| 134 | + expr.span, |
| 135 | + "unnecessary hashes around raw string literal", |
| 136 | + "try", |
| 137 | + format!(r#"{prefix}{hashes}"{}"{hashes}"#, lit.symbol), |
| 138 | + Applicability::MachineApplicable, |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments