Skip to content

Commit 9e93fb3

Browse files
Improve code and apply suggestions
1 parent e98487d commit 9e93fb3

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

clippy_lints/src/useless_concat.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::macro_backtrace;
33
use clippy_utils::paths::CONCAT;
44
use clippy_utils::source::snippet_opt;
5-
use clippy_utils::{match_def_path, tokenize_with_text};
5+
use clippy_utils::tokenize_with_text;
66
use rustc_ast::LitKind;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind};
@@ -43,7 +43,7 @@ impl LateLintPass<'_> for UselessConcat {
4343
// Get the direct parent of the expression.
4444
&& let Some(macro_call) = macro_backtrace(expr.span).next()
4545
// Check if the `concat` macro from the `core` library.
46-
&& match_def_path(cx, macro_call.def_id, &CONCAT)
46+
&& CONCAT.matches(cx, macro_call.def_id)
4747
// We get the original code to parse it.
4848
&& let Some(original_code) = snippet_opt(cx, macro_call.span)
4949
// This check allows us to ensure that the code snippet:
@@ -82,12 +82,14 @@ impl LateLintPass<'_> for UselessConcat {
8282
}
8383
}
8484
let literal = match literal {
85-
Some(lit) => {
85+
Some(original_lit) => {
8686
// Literals can also be number, so we need to check this case too.
87+
let lit = original_lit.strip_prefix("r").unwrap_or(original_lit);
88+
let lit = lit.trim_start_matches('#');
8789
if lit.starts_with('"') {
88-
lit.to_string()
90+
original_lit.to_string()
8991
} else {
90-
format!("\"{lit}\"")
92+
format!("\"{original_lit}\"")
9193
}
9294
},
9395
None => "\"\"".to_string(),

clippy_utils/src/paths.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,14 @@ path_macros! {
129129
// Paths in `core`/`alloc`/`std`. This should be avoided and cleaned up by adding diagnostic items.
130130
pub static ALIGN_OF: PathLookup = value_path!(core::mem::align_of);
131131
pub static CHAR_TO_DIGIT: PathLookup = value_path!(char::to_digit);
132-
pub static CONCAT: PathLookup = value_path!(core::macros::builtin::concat);
133132
pub static IO_ERROR_NEW: PathLookup = value_path!(std::io::Error::new);
134133
pub static IO_ERRORKIND_OTHER_CTOR: PathLookup = value_path!(std::io::ErrorKind::Other);
135134
pub static ITER_STEP: PathLookup = type_path!(core::iter::Step);
136135
pub static SLICE_FROM_REF: PathLookup = value_path!(core::slice::from_ref);
137136

137+
// This path is wrongly computed when using `PathLookup`.
138+
pub static CONCAT: PathLookup = macro_path!(core::concat);
139+
138140
// Paths in external crates
139141
pub static FUTURES_IO_ASYNCREADEXT: PathLookup = type_path!(futures_util::AsyncReadExt);
140142
pub static FUTURES_IO_ASYNCWRITEEXT: PathLookup = type_path!(futures_util::AsyncWriteExt);

tests/ui/useless_concat.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
//@aux-build:proc_macros.rs
2+
13
#![warn(clippy::useless_concat)]
24
#![allow(clippy::print_literal)]
35

6+
extern crate proc_macros;
7+
use proc_macros::{external, with_span};
8+
49
macro_rules! my_concat {
510
($fmt:literal $(, $e:expr)*) => {
611
println!(concat!("ERROR: ", $fmt), $($e,)*);
@@ -10,11 +15,18 @@ macro_rules! my_concat {
1015
fn main() {
1116
let x = ""; //~ useless_concat
1217
let x = "a"; //~ useless_concat
18+
let x = r##"a"##; //~ useless_concat
1319
let x = "1"; //~ useless_concat
1420
println!("b: {}", "a"); //~ useless_concat
1521
// Should not lint.
1622
let x = concat!("a", "b");
1723
let local_i32 = 1;
1824
my_concat!("{}", local_i32);
1925
let x = concat!(file!(), "#L", line!());
26+
27+
external! { concat!(); }
28+
with_span! {
29+
span
30+
concat!();
31+
}
2032
}

tests/ui/useless_concat.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
//@aux-build:proc_macros.rs
2+
13
#![warn(clippy::useless_concat)]
24
#![allow(clippy::print_literal)]
35

6+
extern crate proc_macros;
7+
use proc_macros::{external, with_span};
8+
49
macro_rules! my_concat {
510
($fmt:literal $(, $e:expr)*) => {
611
println!(concat!("ERROR: ", $fmt), $($e,)*);
@@ -10,11 +15,18 @@ macro_rules! my_concat {
1015
fn main() {
1116
let x = concat!(); //~ useless_concat
1217
let x = concat!("a"); //~ useless_concat
18+
let x = concat!(r##"a"##); //~ useless_concat
1319
let x = concat!(1); //~ useless_concat
1420
println!("b: {}", concat!("a")); //~ useless_concat
1521
// Should not lint.
1622
let x = concat!("a", "b");
1723
let local_i32 = 1;
1824
my_concat!("{}", local_i32);
1925
let x = concat!(file!(), "#L", line!());
26+
27+
external! { concat!(); }
28+
with_span! {
29+
span
30+
concat!();
31+
}
2032
}

tests/ui/useless_concat.stderr

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unneeded use of `concat!` macro
2-
--> tests/ui/useless_concat.rs:11:13
2+
--> tests/ui/useless_concat.rs:16:13
33
|
44
LL | let x = concat!();
55
| ^^^^^^^^^ help: replace with: `""`
@@ -8,22 +8,28 @@ LL | let x = concat!();
88
= help: to override `-D warnings` add `#[allow(clippy::useless_concat)]`
99

1010
error: unneeded use of `concat!` macro
11-
--> tests/ui/useless_concat.rs:12:13
11+
--> tests/ui/useless_concat.rs:17:13
1212
|
1313
LL | let x = concat!("a");
1414
| ^^^^^^^^^^^^ help: replace with: `"a"`
1515

1616
error: unneeded use of `concat!` macro
17-
--> tests/ui/useless_concat.rs:13:13
17+
--> tests/ui/useless_concat.rs:18:13
18+
|
19+
LL | let x = concat!(r##"a"##);
20+
| ^^^^^^^^^^^^^^^^^ help: replace with: `r##"a"##`
21+
22+
error: unneeded use of `concat!` macro
23+
--> tests/ui/useless_concat.rs:19:13
1824
|
1925
LL | let x = concat!(1);
2026
| ^^^^^^^^^^ help: replace with: `"1"`
2127

2228
error: unneeded use of `concat!` macro
23-
--> tests/ui/useless_concat.rs:14:23
29+
--> tests/ui/useless_concat.rs:20:23
2430
|
2531
LL | println!("b: {}", concat!("a"));
2632
| ^^^^^^^^^^^^ help: replace with: `"a"`
2733

28-
error: aborting due to 4 previous errors
34+
error: aborting due to 5 previous errors
2935

0 commit comments

Comments
 (0)