-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add new useless_concat
lint
#13829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samueltardieu
merged 4 commits into
rust-lang:master
from
GuillaumeGomez:useless-concat
May 19, 2025
Merged
Add new useless_concat
lint
#13829
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::macros::macro_backtrace; | ||
use clippy_utils::paths::CONCAT; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::tokenize_with_text; | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lexer::TokenKind; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks that the `concat!` macro has at least two arguments. | ||
/// | ||
/// ### Why is this bad? | ||
/// If there are less than 2 arguments, then calling the macro is doing nothing. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// let x = concat!("a"); | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let x = "a"; | ||
/// ``` | ||
#[clippy::version = "1.89.0"] | ||
pub USELESS_CONCAT, | ||
complexity, | ||
"checks that the `concat` macro has at least two arguments" | ||
} | ||
|
||
declare_lint_pass!(UselessConcat => [USELESS_CONCAT]); | ||
|
||
impl LateLintPass<'_> for UselessConcat { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
// Check that the expression is generated by a macro. | ||
if expr.span.from_expansion() | ||
// Check that it's a string literal. | ||
&& let ExprKind::Lit(lit) = expr.kind | ||
&& let LitKind::Str(lit_s, _) = lit.node | ||
// Get the direct parent of the expression. | ||
&& let Some(macro_call) = macro_backtrace(expr.span).next() | ||
// Check if the `concat` macro from the `core` library. | ||
&& CONCAT.matches(cx, macro_call.def_id) | ||
// We get the original code to parse it. | ||
&& let Some(original_code) = snippet_opt(cx, macro_call.span) | ||
// This check allows us to ensure that the code snippet: | ||
// 1. Doesn't come from proc-macro expansion. | ||
// 2. Doesn't come from foreign macro expansion. | ||
// | ||
// It works as follows: if the snippet we get doesn't contain `concat!(`, then it | ||
// means it's not code written in the current crate so we shouldn't lint. | ||
&& let mut parts = original_code.split('!') | ||
&& parts.next().is_some_and(|p| p.trim() == "concat") | ||
&& parts.next().is_some_and(|p| p.trim().starts_with('(')) | ||
{ | ||
let mut literal = None; | ||
let mut nb_commas = 0; | ||
let mut nb_idents = 0; | ||
for (token_kind, token_s, _) in tokenize_with_text(&original_code) { | ||
match token_kind { | ||
TokenKind::Eof => break, | ||
TokenKind::Literal { .. } => { | ||
if literal.is_some() { | ||
return; | ||
} | ||
literal = Some(token_s); | ||
}, | ||
TokenKind::Ident => { | ||
if token_s == "true" || token_s == "false" { | ||
literal = Some(token_s); | ||
} else { | ||
nb_idents += 1; | ||
} | ||
}, | ||
TokenKind::Comma => { | ||
nb_commas += 1; | ||
if nb_commas > 1 { | ||
return; | ||
} | ||
}, | ||
// We're inside a macro definition and we are manipulating something we likely | ||
// shouldn't, so aborting. | ||
TokenKind::Dollar => return, | ||
_ => {}, | ||
} | ||
} | ||
// There should always be the ident of the `concat` macro. | ||
if nb_idents == 1 { | ||
span_lint_and_sugg( | ||
cx, | ||
USELESS_CONCAT, | ||
macro_call.span, | ||
"unneeded use of `concat!` macro", | ||
"replace with", | ||
format!("{lit_s:?}"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//@aux-build:proc_macros.rs | ||
|
||
#![warn(clippy::useless_concat)] | ||
#![allow(clippy::print_literal)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::{external, with_span}; | ||
|
||
macro_rules! my_concat { | ||
($fmt:literal $(, $e:expr)*) => { | ||
println!(concat!("ERROR: ", $fmt), $($e,)*); | ||
} | ||
} | ||
|
||
fn main() { | ||
let x = ""; //~ useless_concat | ||
let x = "c"; //~ useless_concat | ||
let x = "\""; //~ useless_concat | ||
let x = "true"; //~ useless_concat | ||
let x = "1"; //~ useless_concat | ||
let x = "1.0000"; //~ useless_concat | ||
let x = "1"; //~ useless_concat | ||
let x = "1"; //~ useless_concat | ||
let x = "1.0000"; //~ useless_concat | ||
let x = "1.0000"; //~ useless_concat | ||
let x = "a😀\n"; //~ useless_concat | ||
let x = "a"; //~ useless_concat | ||
let x = "1"; //~ useless_concat | ||
println!("b: {}", "a"); //~ useless_concat | ||
// Should not lint. | ||
let x = concat!("a", "b"); | ||
let local_i32 = 1; | ||
my_concat!("{}", local_i32); | ||
let x = concat!(file!(), "#L", line!()); | ||
|
||
external! { concat!(); } | ||
with_span! { | ||
span | ||
concat!(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//@aux-build:proc_macros.rs | ||
|
||
#![warn(clippy::useless_concat)] | ||
#![allow(clippy::print_literal)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::{external, with_span}; | ||
|
||
macro_rules! my_concat { | ||
($fmt:literal $(, $e:expr)*) => { | ||
println!(concat!("ERROR: ", $fmt), $($e,)*); | ||
} | ||
} | ||
|
||
fn main() { | ||
let x = concat!(); //~ useless_concat | ||
let x = concat!('c'); //~ useless_concat | ||
let x = concat!('"'); //~ useless_concat | ||
let x = concat!(true); //~ useless_concat | ||
let x = concat!(1f32); //~ useless_concat | ||
let x = concat!(1.0000f32); //~ useless_concat | ||
let x = concat!(1_f32); //~ useless_concat | ||
let x = concat!(1_); //~ useless_concat | ||
let x = concat!(1.0000_f32); //~ useless_concat | ||
let x = concat!(1.0000_); //~ useless_concat | ||
let x = concat!("a\u{1f600}\n"); //~ useless_concat | ||
let x = concat!(r##"a"##); //~ useless_concat | ||
let x = concat!(1); //~ useless_concat | ||
println!("b: {}", concat!("a")); //~ useless_concat | ||
// Should not lint. | ||
let x = concat!("a", "b"); | ||
let local_i32 = 1; | ||
my_concat!("{}", local_i32); | ||
let x = concat!(file!(), "#L", line!()); | ||
|
||
external! { concat!(); } | ||
with_span! { | ||
span | ||
concat!(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:16:13 | ||
| | ||
LL | let x = concat!(); | ||
| ^^^^^^^^^ help: replace with: `""` | ||
| | ||
= note: `-D clippy::useless-concat` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::useless_concat)]` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:17:13 | ||
| | ||
LL | let x = concat!('c'); | ||
| ^^^^^^^^^^^^ help: replace with: `"c"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:18:13 | ||
| | ||
LL | let x = concat!('"'); | ||
| ^^^^^^^^^^^^ help: replace with: `"\""` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:19:13 | ||
| | ||
LL | let x = concat!(true); | ||
| ^^^^^^^^^^^^^ help: replace with: `"true"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:20:13 | ||
| | ||
LL | let x = concat!(1f32); | ||
| ^^^^^^^^^^^^^ help: replace with: `"1"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:21:13 | ||
| | ||
LL | let x = concat!(1.0000f32); | ||
| ^^^^^^^^^^^^^^^^^^ help: replace with: `"1.0000"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:22:13 | ||
| | ||
LL | let x = concat!(1_f32); | ||
| ^^^^^^^^^^^^^^ help: replace with: `"1"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:23:13 | ||
| | ||
LL | let x = concat!(1_); | ||
| ^^^^^^^^^^^ help: replace with: `"1"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:24:13 | ||
| | ||
LL | let x = concat!(1.0000_f32); | ||
| ^^^^^^^^^^^^^^^^^^^ help: replace with: `"1.0000"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:25:13 | ||
| | ||
LL | let x = concat!(1.0000_); | ||
| ^^^^^^^^^^^^^^^^ help: replace with: `"1.0000"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:26:13 | ||
| | ||
LL | let x = concat!("a\u{1f600}\n"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `"a😀\n"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:27:13 | ||
| | ||
LL | let x = concat!(r##"a"##); | ||
| ^^^^^^^^^^^^^^^^^ help: replace with: `"a"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:28:13 | ||
| | ||
LL | let x = concat!(1); | ||
| ^^^^^^^^^^ help: replace with: `"1"` | ||
|
||
error: unneeded use of `concat!` macro | ||
--> tests/ui/useless_concat.rs:29:23 | ||
| | ||
LL | println!("b: {}", concat!("a")); | ||
| ^^^^^^^^^^^^ help: replace with: `"a"` | ||
|
||
error: aborting due to 14 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.