rust-lang / rust-lang/rust-clippy
redundant-else: reduced scope clashes with same-name statics
Open
Nobody has claimed this yet.
C-bug
I-suggestion-causes-error
P-low
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Using the following flags
--force-warn clippy::redundant-else
this code:
pub fn foo<P: ?Sized>() -> &'static isize {
if false {
static a: isize = 4;
if false {
static a: isize = 4;
return &a;
} else {
static a: isize = 5;
return &a;
}
} else {
static a: isize = 5;
return &a;
}
}
pub fn bar() -> &'static isize {
foo::<isize>()
}
caused the following diagnostics:
Checking _45633816536bcb2ed004a146e225178f170fb852 v0.1.0 (/tmp/icemaker_global_tempdir.tMrpGeJ6WWUI/icemaker_clippyfix_tempdir.FuaahGfV2Y1d/_45633816536bcb2ed004a146e225178f170fb852)
warning: redundant else block
--> src/lib.rs:11:6
|
11 | } else {
| ______^
12 | | static a: isize = 5;
13 | | return &a;
14 | | }
| |_____^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else
= note: requested on the command line with `--force-warn clippy::redundant-else`
help: remove the `else` block and move the contents out
|
11 ~ }
12 + static a: isize = 5;
13 + return &a;
|
warning: redundant else block
--> src/lib.rs:7:6
|
7 | } else {
| ______^
8 | | static a: isize = 5;
9 | | return &a;
10 | | }
| |_____^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else
help: remove the `else` block and move the contents out
|
7 ~ }
8 + static a: isize = 5;
9 + return &a;
|
warning: `_45633816536bcb2ed004a146e225178f170fb852` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p _45633816536bcb2ed004a146e225178f170fb852` to apply 2 suggestions)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s
However after applying these diagnostics, the resulting code:
pub fn foo<P: ?Sized>() -> &'static isize {
if false {
static a: isize = 4;
if false {
static a: isize = 4;
return &a;
}
static a: isize = 5;
return &a;
}
static a: isize = 5;
return &a;
}
pub fn bar() -> &'static isize {
foo::<isize>()
}
no longer compiled:
Checking _45633816536bcb2ed004a146e225178f170fb852 v0.1.0 (/tmp/icemaker_global_tempdir.tMrpGeJ6WWUI/icemaker_clippyfix_tempdir.FuaahGfV2Y1d/_45633816536bcb2ed004a146e225178f170fb852)
error[E0428]: the name `a` is defined multiple times
--> src/lib.rs:8:9
|
3 | static a: isize = 4;
| -------------------- previous definition of the value `a` here
...
8 | static a: isize = 5;
| ^^^^^^^^^^^^^^^^^^^^ `a` redefined here
|
= note: `a` must be defined only once in the value namespace of this block
For more information about this error, try `rustc --explain E0428`.
error: could not compile `_45633816536bcb2ed004a146e225178f170fb852` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_45633816536bcb2ed004a146e225178f170fb852` (lib test) due to 1 previous error
Version:
rustc 1.93.0-nightly (6a884ad1b 2025-11-02)
binary: rustc
commit-hash: 6a884ad1b502fe48307d363858510702429fc735
commit-date: 2025-11-02
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the supplied Rust example with clippy::redundant-else and the cargo clippy --fix flow. Trace the redundant-else lint's suggestion generation, then verify that applying the suggestion preserves compilation and does not create duplicate same-name statics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100