Skip to content

Commit 42f4fd6

Browse files
Improve code and apply suggestions
1 parent dd0d88a commit 42f4fd6

File tree

6 files changed

+41
-17
lines changed

6 files changed

+41
-17
lines changed

clippy_lints/src/useless_concat.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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_lints_internal/src/invalid_paths.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
6363
if !def_path_res(cx.tcx, path).is_empty() {
6464
return true;
6565
}
66-
// `builtin` macros don't seem to be found in `def_path_res`...
67-
if path == ["core", "macros", "builtin", "concat"] {
68-
return true;
69-
}
7066

7167
// Some implementations can't be found by `path_to_res`, particularly inherent
7268
// implementations of native types. Check lang items.

clippy_utils/src/paths.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
2727
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
2828

2929
// Paths in `core`/`alloc`/`std`. This should be avoided and cleaned up by adding diagnostic items.
30-
pub const ABORT: [&str; 3] = ["std", "process", "abort"];
31-
pub const CHILD: [&str; 3] = ["std", "process", "Child"];
32-
pub const CHILD_ID: [&str; 4] = ["std", "process", "Child", "id"];
33-
pub const CHILD_KILL: [&str; 4] = ["std", "process", "Child", "kill"];
34-
pub const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
30+
#[expect(clippy::invalid_paths)]
3531
pub const CONCAT: [&str; 4] = ["core", "macros", "builtin", "concat"];
3632
pub const CHAR_IS_ASCII: [&str; 5] = ["core", "char", "methods", "<impl char>", "is_ascii"];
3733
pub const IO_ERROR_NEW: [&str; 5] = ["std", "io", "error", "Error", "new"];

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)