add needless_raw_string_hashes lint
add semicolon in doctest
This commit is contained in:
parent
ecdea8cdd3
commit
1bf74fc303
18 changed files with 216 additions and 22 deletions
|
|
@ -469,6 +469,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
|
|||
crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO,
|
||||
crate::needless_pass_by_value::NEEDLESS_PASS_BY_VALUE_INFO,
|
||||
crate::needless_question_mark::NEEDLESS_QUESTION_MARK_INFO,
|
||||
crate::needless_raw_string_hashes::NEEDLESS_RAW_STRING_HASHES_INFO,
|
||||
crate::needless_update::NEEDLESS_UPDATE_INFO,
|
||||
crate::neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD_INFO,
|
||||
crate::neg_multiply::NEG_MULTIPLY_INFO,
|
||||
|
|
|
|||
|
|
@ -230,6 +230,7 @@ mod needless_late_init;
|
|||
mod needless_parens_on_range_literals;
|
||||
mod needless_pass_by_value;
|
||||
mod needless_question_mark;
|
||||
mod needless_raw_string_hashes;
|
||||
mod needless_update;
|
||||
mod neg_cmp_op_on_partial_ord;
|
||||
mod neg_multiply;
|
||||
|
|
|
|||
73
clippy_lints/src/needless_raw_string_hashes.rs
Normal file
73
clippy_lints/src/needless_raw_string_hashes.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use rustc_ast::{
|
||||
ast::{Expr, ExprKind},
|
||||
token::LitKind,
|
||||
};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
/// Checks for raw string literals with an unnecessary amount of hashes around them.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// It's just unnecessary, and makes it look like there's more escaping needed than is actually
|
||||
/// necessary.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```rust
|
||||
/// let r = r###"Hello, "world"!"###;
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// let r = r#"Hello, "world"!"#;
|
||||
/// ```
|
||||
#[clippy::version = "1.72.0"]
|
||||
pub NEEDLESS_RAW_STRING_HASHES,
|
||||
complexity,
|
||||
"suggests reducing the number of hashes around a raw string literal"
|
||||
}
|
||||
declare_lint_pass!(NeedlessRawStringHashes => [NEEDLESS_RAW_STRING_HASHES]);
|
||||
|
||||
impl EarlyLintPass for NeedlessRawStringHashes {
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||
if_chain! {
|
||||
if !in_external_macro(cx.sess(), expr.span);
|
||||
if let ExprKind::Lit(lit) = expr.kind;
|
||||
if let LitKind::StrRaw(num) | LitKind::ByteStrRaw(num) | LitKind::CStrRaw(num) = lit.kind;
|
||||
then {
|
||||
let str = lit.symbol.as_str();
|
||||
let mut lowest = 0;
|
||||
|
||||
for i in (0..num).rev() {
|
||||
if str.contains(&format!("\"{}", "#".repeat(i as usize))) {
|
||||
lowest = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if lowest < num {
|
||||
let hashes = "#".repeat(lowest as usize);
|
||||
let prefix = match lit.kind {
|
||||
LitKind::StrRaw(..) => "r",
|
||||
LitKind::ByteStrRaw(..) => "br",
|
||||
LitKind::CStrRaw(..) => "cr",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
NEEDLESS_RAW_STRING_HASHES,
|
||||
expr.span,
|
||||
"unnecessary hashes around raw string literal",
|
||||
"try",
|
||||
format!(r#"{prefix}{hashes}"{}"{hashes}"#, lit.symbol),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue