Skip to content

False branches_sharing_code for bodies with macro call(s) #14873

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

Open
msix29 opened this issue May 22, 2025 · 4 comments · May be fixed by #14907
Open

False branches_sharing_code for bodies with macro call(s) #14873

msix29 opened this issue May 22, 2025 · 4 comments · May be fixed by #14907
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have L-nursery Lint: Currently in the nursery group

Comments

@msix29
Copy link

msix29 commented May 22, 2025

Summary

If needed, this is the file (currently editing - line numbers given by the lint may be off by a small amount).

Lint Name

branches_sharing_code

Reproducer

Given:

let equal_or_colon = if parse_args.is_type {
    maybe_next_token!(lexer, temp, TokenType::Symbol(Symbol::Colon));
    temp
} else {
    maybe_next_token!(lexer, temp, TokenType::Symbol(Symbol::Equal));
    temp
};

The maybe_next_token! macro takes a pat as the final argument (thus this can't be refactored). Clippy thinks both macro calls are the same when they clearly aren't. Full message:

all if blocks contain the same code at the start
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
`#[warn(clippy::branches_sharing_code)]` implied by `#[warn(clippy::nursery)]`clippy[branches_sharing_code](rust-analyzer-diagnostics-view:/diagnostic%20message%20[3]?3#file:///home/Ryo/dev/rust/luau/parser/src/impl/value/table.rs)

and

table.rs(113, 21): consider moving these statements before the if: `maybe_next_token!(lexer, temp, TokenType::Symbol(Symbol::Colon));
let equal_or_colon = if parse_args.is_type {`

Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Additional Labels

No response

@msix29 msix29 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 22, 2025
@samueltardieu
Copy link
Contributor

samueltardieu commented May 23, 2025

@rustbot label +L-nursery

@rustbot rustbot added the L-nursery Lint: Currently in the nursery group label May 23, 2025
@donkomura
Copy link
Contributor

@msix29 Cloud you please give us a simplified reproducer?

@msix29
Copy link
Author

msix29 commented May 27, 2025

#![warn(clippy::nursery)]

fn foo() -> i32 {
    todo!()
}
fn bar() -> i32 {
    todo!()
}

macro_rules! qux {
    ($a:ident, $b:ident, $condition:expr) => {
        let $a = foo();
        let $b = bar();

        if $condition {
            "." // any random value to avoid other warnings.
        } else {
            ""
        }
    };
}

fn main() {
    let _ = if false {
        qux!(a, b, a == b);
    } else {
        qux!(a, b, a != b);
    };
}

The error would be valid if it isn't a macro call, but being a macro call, you can't really do anything about it.

@profetia
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have L-nursery Lint: Currently in the nursery group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants