Skip to content

Don't suggest fixes that cut through a macro call - #18035

Open
Itz-Agasta wants to merge 6 commits into
rust-lang:mainfrom
Itz-Agasta:fix-18018-macro-span-balance
Open

Itz-Agasta wants to merge 6 commits into
rust-lang:mainfrom
Itz-Agasta:fix-18018-macro-span-balance

Conversation

@Itz-Agasta

@Itz-Agasta Itz-Agasta commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Part of #18018

lints that build their replacement span from a macro argument (eg. v in id!(v)) can cut through the macro call... so added clippy_utils::span_has_balanced_delims (lexes the span's source and checks that every (/[/{ is closed inside it). The lints below now skip when the span isn't balanced.

fixes #18006
fixes #18007
fixes #18008
fixes #18009
fixes #18010

used agents to comeup with some diff diff styles of test cases, rest of the work specially clippy_utils done by me :(


changelog: [iter_cloned_collect], [bool_comparison], [manual_is_variant_and], [collapsible_match], [blocks_in_conditions]: don't lint when the suggestion would cut through a macro call

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Nov 9, 2026
@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 9, 2026
@Itz-Agasta

Copy link
Copy Markdown
Contributor Author

dont want to blot this prs scop... & sadly i cant give a stack prs... so #18005 (the let is inserted inside the macro call, a different problem) and #17997 (grouping lost when the arm body is wrapped in { }). will do prs separately for them.

@Itz-Agasta Itz-Agasta changed the title Fix 18018 macro span balance Don't suggest fixes that cut through a macro call Nov 9, 2026
@CommanderStorm CommanderStorm added the llm-assisted For PRs that were partially or completely done with assistance of AI/LLM tools label Nov 9, 2026

@workingjubilee workingjubilee left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

community review: hi, as a rustc dev I made sure that we never used issue{N} as a name for a test file ever again. clippy has its own standards about naming things, but let's just say that this kind of name is a good way to create work for someone else to research and clean up later. rust-lang/compiler-team#658

View changes since this review

Comment thread tests/ui/collapsible_match_fixable.rs Resolved
Comment thread tests/ui/bool_comparison.rs Resolved
Comment thread tests/ui/blocks_in_conditions.rs Resolved
Comment thread tests/ui/iter_cloned_collect.rs Resolved
Comment thread tests/ui/manual_is_variant_and.rs Resolved
@Itz-Agasta
Itz-Agasta force-pushed the fix-18018-macro-span-balance branch 2 times, most recently from d0a03d6 to 8149b3d Compare November 9, 2026 20:29
@Itz-Agasta

Copy link
Copy Markdown
Contributor Author

community review: hi, as a rustc dev I made sure that we never used issue{N} as a name for a test file ever again. clippy has its own standards about naming things, but let's just say that this kind of name is a good way to create work for someone else to research and clean up later. rust-lang/compiler-team#658

View changes since this review

Make sense .. I myself am not a big fan of this style either. I have already disclosed that tests were made by agent... so when I was doing the local review I noticed so many earlier tests too were written this way that's why ignored it.... But yeah, have a look now

Comment thread tests/ui/manual_is_variant_and.rs Resolved
@blyxyas

blyxyas commented Nov 9, 2026

Copy link
Copy Markdown
Member

have already disclosed that tests were made by agent

Please take into account that this is your code, and you have full responsibility of it.

@Itz-Agasta

Itz-Agasta commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

have already disclosed that tests were made by agent

Please take into account that this is your code, and you have full responsibility for it.

Yes I'm aware of that ... I'm kinda getting used to the repo standards.. as this style is already there in earlier so many tests written by others so I thought this might be the way that's why ignored that ... That said yes pardon me, i will keep a good eye on these from next time 🫡

@Itz-Agasta

Copy link
Copy Markdown
Contributor Author

Btw @blyxyas I was looking at the issue and the things @workingjubilee mentioned.... rustc has this rule written down in the contributor guide also a tidy check.....

Won't it be good if Clippy has it too like in writing test.md or in contributing.md?

@workingjubilee workingjubilee left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cool! that looks better.

View changes since this review

@workingjubilee

Copy link
Copy Markdown
Member

probably better to put that stuff in a separate PR, people like to haggle over it when it gets written down as a rule whereas if the clippy code is fine this PR is fine. (I only looked at the added tests, and they're mostly the same as the stuff in the issues.)

@Itz-Agasta

Copy link
Copy Markdown
Contributor Author

probably better to put that stuff in a separate PR, people like to haggle over it when it gets written down as a rule whereas if the clippy code is fine this PR is fine.

got it... i believe this rule/suggesion should naturally go in book/src/dev/writing_tests.md .. will do a pr for it 👍

@Itz-Agasta

Copy link
Copy Markdown
Contributor Author

hey @CommanderStorm if y got some time can y do the 2nd community review pls 👉👈 thanks

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not sure about span_has_balanced_delims it seems like we are lumping things together, or what is going on there?

View changes since this review

Comment thread clippy_utils/src/lib.rs Resolved
@Itz-Agasta

Itz-Agasta commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

I am not sure about span_has_balanced_delims it seems like we are lumping things together, or what is going on there?

View changes since this review

ok so my single counter works because rustc already rejects mismatched brackets & that's not obvious/ readable from my code i believe. (i did some back and forth on this earlier, before pusing one counter so yes it works)

your three counters still let ([)] through, though. yk how about a stack? It will check each closing bracket against the last open one:

let mut open = Vec::new();
for token in tokenize(snippet, FrontmatterAllowed::No) {
    let expected = match token.kind {
        TokenKind::OpenParen | TokenKind::OpenBracket | TokenKind::OpenBrace => {
            open.push(token.kind);
            continue;
        },
        TokenKind::CloseParen => TokenKind::OpenParen,
        TokenKind::CloseBracket => TokenKind::OpenBracket,
        TokenKind::CloseBrace => TokenKind::OpenBrace,
        _ => continue,
    };
    if open.pop() != Some(expected) {
        return false;
    }
}
open.is_empty()

same results & little easier to follow (lol i just remember i was revising this dsa pattern few weeks ago 😹 ). i mean its kinda opinionated on coding style..fine with whichever you prefer

@CommanderStorm CommanderStorm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤦‍♂️ totally did not consider that this is a precondition

View changes since this review

@rustbot rustbot removed the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Nov 11, 2026
@rustbot

rustbot commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu for the project review.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 9 candidates
  • 9 candidates expanded to 9 candidates
  • Random selection from 6 candidates

@CommanderStorm

CommanderStorm commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

On zulip (#t-clippy > Remaining (yet-unreported) fuzzing issues) these two were reported too.

Important

These are LLM/fuzzer/? findings, so really no need to take action at all! ^^

They seem like the same fix that you already applied here, so I am mostly commenting in case you want to fix them here too, I can also do an followup for this..
seems simple enough ^^

  • unnecessary_literal_unwrap
    macro_rules! id {
        ($e:expr) => {
            $e
        };
    }
      
    let x = id!(Some(1)).unwrap();
  • unnecessary_to_owned
    macro_rules! id {
      ($e:expr) => {
          $e
      };
    }
    
    let b: &[u8] = b"hi";
    let s: &str = &id!(String::from_utf8(b.to_vec()).unwrap());

blyxyas added a commit to profetia/rust-clippy that referenced this pull request Nov 18, 2026
Follow-up to the
[discussion](rust-lang#18035 (review))
in rust-lang#18035.

Adds a short "Naming Test Cases" section to the testing chapter of the
book
CC: rust-lang/compiler-team#658

changelog: none
flip1995 pushed a commit to flip1995/rust that referenced this pull request Nov 20, 2026
Follow-up to the
[discussion](rust-lang/rust-clippy#18035 (review))
in rust-lang/rust-clippy#18035.

Adds a short "Naming Test Cases" section to the testing chapter of the
book
CC: rust-lang/compiler-team#658

changelog: none
@Jarcho

Jarcho commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

The problem with all the suggestions is the span adjustments are just outright incorrect. I have various instances of this fixed (e.g. collapsible_match), but those are waiting on #14724. I don't hate this approach as a stop-gap measure until the span adjustments are fixed, but I would only want to do it if people are actually encountering these. If these are only encountered while fuzzing then there's no pressing reason to have a stop-gap measure.

For reference the fix for collapsible_match involved rewriting most of the lint. Proper fixes for these problems can be non-trivial and doing a check for balanced parenthesis is relatively trivial to add.

Comment thread clippy_utils/src/lib.rs Resolved
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Nov 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Itz-Agasta
Itz-Agasta force-pushed the fix-18018-macro-span-balance branch 3 times, most recently from 28a661c to 07e4991 Compare November 22, 2026 15:47
@Itz-Agasta
Itz-Agasta force-pushed the fix-18018-macro-span-balance branch from 07e4991 to 8277c59 Compare November 22, 2026 15:59
@rustbot

rustbot commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Itz-Agasta

Copy link
Copy Markdown
Contributor Author

The problem with all the suggestions is the span adjustments are just outright incorrect. I have various instances of this fixed (e.g. collapsible_match), but those are waiting on #14724. I don't hate this approach as a stop-gap measure until the span adjustments are fixed, but I would only want to do it if people are actually encountering these. If these are only encountered while fuzzing then there's no pressing reason to have a stop-gap measure.

mmmm looked...span adjustments is the right call too ig,,,,so yaa should i close this pr? or keep it as a temp stop-gap :(

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted For PRs that were partially or completely done with assistance of AI/LLM tools S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

7 participants