raw_strings: handle format template as well

This commit is contained in:
Samuel Tardieu 2024-10-05 15:58:30 +02:00
parent 846bd30cfe
commit 5e78c15caa
7 changed files with 130 additions and 3 deletions

View File

@ -1,6 +1,6 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::{SpanRangeExt, snippet_opt};
use rustc_ast::ast::{Expr, ExprKind};
use rustc_ast::token::LitKind;
use rustc_errors::Applicability;
@ -71,6 +71,23 @@ pub fn new(conf: &'static Conf) -> Self {
impl EarlyLintPass for RawStrings {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::FormatArgs(format_args) = &expr.kind
&& !in_external_macro(cx.sess(), format_args.span)
&& format_args.span.check_source_text(cx, |src| src.starts_with('r'))
&& let Some(str) = snippet_opt(cx.sess(), format_args.span)
&& let count_hash = str.bytes().skip(1).take_while(|b| *b == b'#').count()
&& let Some(str) = str.get(count_hash + 2..str.len() - count_hash - 1)
{
self.check_raw_string(
cx,
str,
format_args.span,
"r",
u8::try_from(count_hash).unwrap(),
"string",
);
}
if let ExprKind::Lit(lit) = expr.kind
&& let (prefix, max) = match lit.kind {
LitKind::StrRaw(max) => ("r", max),

View File

@ -22,3 +22,12 @@ fn main() {
b"no hashes";
c"no hashes";
}
fn issue_13503() {
println!("SELECT * FROM posts");
println!("SELECT * FROM posts");
println!(r##"SELECT * FROM "posts""##);
// Test arguments as well
println!("{}", "foobar".len());
}

View File

@ -22,3 +22,12 @@ fn main() {
br"no hashes";
cr"no hashes";
}
fn issue_13503() {
println!(r"SELECT * FROM posts");
println!(r#"SELECT * FROM posts"#);
println!(r##"SELECT * FROM "posts""##);
// Test arguments as well
println!("{}", r"foobar".len());
}

View File

@ -91,5 +91,41 @@ LL - cr"no hashes";
LL + c"no hashes";
|
error: aborting due to 7 previous errors
error: unnecessary raw string literal
--> tests/ui/needless_raw_string.rs:27:14
|
LL | println!(r"SELECT * FROM posts");
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: use a plain string literal instead
|
LL - println!(r"SELECT * FROM posts");
LL + println!("SELECT * FROM posts");
|
error: unnecessary raw string literal
--> tests/ui/needless_raw_string.rs:28:14
|
LL | println!(r#"SELECT * FROM posts"#);
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use a plain string literal instead
|
LL - println!(r#"SELECT * FROM posts"#);
LL + println!("SELECT * FROM posts");
|
error: unnecessary raw string literal
--> tests/ui/needless_raw_string.rs:32:20
|
LL | println!("{}", r"foobar".len());
| ^^^^^^^^^
|
help: use a plain string literal instead
|
LL - println!("{}", r"foobar".len());
LL + println!("{}", "foobar".len());
|
error: aborting due to 10 previous errors

View File

@ -24,3 +24,13 @@ fn main() {
r"rust";
r"hello world";
}
fn issue_13503() {
println!(r"SELECT * FROM posts");
println!(r"SELECT * FROM posts");
println!(r#"SELECT * FROM "posts""#);
println!(r#"SELECT * FROM "posts""#);
// Test arguments as well
println!("{}", r"foobar".len());
}

View File

@ -24,3 +24,13 @@ fn main() {
r###"rust"###;
r#"hello world"#;
}
fn issue_13503() {
println!(r"SELECT * FROM posts");
println!(r#"SELECT * FROM posts"#);
println!(r##"SELECT * FROM "posts""##);
println!(r##"SELECT * FROM "posts""##);
// Test arguments as well
println!("{}", r"foobar".len());
}

View File

@ -187,5 +187,41 @@ LL - r#"hello world"#;
LL + r"hello world";
|
error: aborting due to 15 previous errors
error: unnecessary hashes around raw string literal
--> tests/ui/needless_raw_string_hashes.rs:30:14
|
LL | println!(r#"SELECT * FROM posts"#);
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove all the hashes around the string literal
|
LL - println!(r#"SELECT * FROM posts"#);
LL + println!(r"SELECT * FROM posts");
|
error: unnecessary hashes around raw string literal
--> tests/ui/needless_raw_string_hashes.rs:31:14
|
LL | println!(r##"SELECT * FROM "posts""##);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove one hash from both sides of the string literal
|
LL - println!(r##"SELECT * FROM "posts""##);
LL + println!(r#"SELECT * FROM "posts""#);
|
error: unnecessary hashes around raw string literal
--> tests/ui/needless_raw_string_hashes.rs:32:14
|
LL | println!(r##"SELECT * FROM "posts""##);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove one hash from both sides of the string literal
|
LL - println!(r##"SELECT * FROM "posts""##);
LL + println!(r#"SELECT * FROM "posts""#);
|
error: aborting due to 18 previous errors