Skip to content
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

Add new useless_concat lint #13829

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

Fixes #13793.

Interestingly enough, to actually check that the macro call has at least two arguments, we need to use the rust lexer after getting the original source code snippet.

changelog: Add new useless_concat lint

@rustbot
Copy link
Collaborator

rustbot commented Dec 14, 2024

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 14, 2024
@GuillaumeGomez GuillaumeGomez force-pushed the useless-concat branch 2 times, most recently from 4918abc to b92d91b Compare December 15, 2024 20:51
@GuillaumeGomez
Copy link
Member Author

Finally was able to only trigger the lint when appropriate.

@GuillaumeGomez
Copy link
Member Author

r? clippy

@rustbot rustbot assigned y21 and unassigned dswij Dec 25, 2024
// 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.
&& match_def_path(cx, macro_call.def_id, &["core", "macros", "builtin", "concat"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you move this path to clippy_utils::paths? Better to have the hardcoded paths in one place so we can every once in a while go through them and make them diagnostic items

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about it, thanks for the tip!

/// ```
#[clippy::version = "1.85.0"]
pub USELESS_CONCAT,
suspicious,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
suspicious,
complexity,

Since it just simplifies an expression and concat! isn't wrong to use, complexity feels more correct

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

if literal.is_some() {
return;
}
literal = Some(&original_code[current_pos..current_pos + token.len as usize]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concat!(1) currently has 1 as the suggestion but it needs to be "1" with quotes to preserve the right type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, gonna fix this case and add a regression test.

{
let mut literal = None;
let mut current_pos = 0;
let mut cursor = Cursor::new(&original_code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can this use tokenize_with_text? It also gives access to the string, so that feels like it could simplify some parts like keeping track of positions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about this API either, quite convenient, thanks!

@GuillaumeGomez
Copy link
Member Author

Fixed dogfood. :)

github-merge-queue bot pushed a commit that referenced this pull request Dec 30, 2024
I discovered that there were paths declared in `clippy_utils::paths` in
#13829 so moving a few
remaining hardcoded ones in one place.

changelog: Move more def paths into `clippy_utils::paths`

r? @y21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add useless_concat
4 participants