Skip to content

Commit

Permalink
raw_strings: handle format template as well
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Oct 7, 2024
1 parent 846bd30 commit 5e78c15
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
19 changes: 18 additions & 1 deletion clippy_lints/src/raw_strings.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,6 +71,23 @@ impl RawStrings {

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),
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/needless_raw_string.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
9 changes: 9 additions & 0 deletions tests/ui/needless_raw_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
38 changes: 37 additions & 1 deletion tests/ui/needless_raw_string.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

10 changes: 10 additions & 0 deletions tests/ui/needless_raw_string_hashes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
10 changes: 10 additions & 0 deletions tests/ui/needless_raw_string_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
38 changes: 37 additions & 1 deletion tests/ui/needless_raw_string_hashes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5e78c15

Please sign in to comment.