Skip to content

redundant-else: reduced scope clashes with same-name statics #16013

@matthiaskrgr

Description

@matthiaskrgr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedP-lowPriority: Low

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions