rust-lang / rust-lang/rust-clippy

`if_same_then_else` triggers when bodies only differ in attributes

Open
#16,356 6 comments 1 reaction 1 assignee View on GitHub

@willwang-io is already working on this.

Since Sep 18, 2026.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy does not understand the defmt::write! macro and emits a lint warning when bodies differ in the interned string's value.

The macro currently expands to code like this:

{
    let _typecheck_formatter: defmt::Formatter<'_> = fmt;
    match () {
        () => {
            defmt::export::istr(&{
                defmt::export::make_istr({
                    #[cfg_attr(target_os = "macos", link_section = ".defmt,119c41d6e2719ed6")]
                    #[cfg_attr(
                        not(target_os = "macos"),
                        link_section = ".defmt.{\"package\":\"esp-alloc\",\"tag\":\"defmt_write\",\"data\":\"Internal\",\"disambiguator\":\"13789615010068757779\",\"crate_name\":\"esp_alloc\"}"
                    )]
                    #[export_name = "{\"package\":\"esp-alloc\",\"tag\":\"defmt_write\",\"data\":\"Internal\",\"disambiguator\":\"13789615010068757779\",\"crate_name\":\"esp_alloc\"}"]
                    static S: u8 = 0;
                    &S as *const u8 as u16
                })
            });
        }
    }
}

The macro expansions only differ in the attributes on static S - this is how defmt interns strings into the ELF without including them in the program binary that is downloaded to embedded devices.

Lint Name

if_same_then_else

Reproducer

I tried this code (the revision already contains the allow for the lint, which we had to add to work around this issue):

https://github.com/esp-rs/esp-hal/blob/ec7048e9ab1ab5014fe1048292bf98afca07e49b/esp-alloc/src/lib.rs#L239-L244

I saw this happen:

error: this `if` has identical blocks
  Error:    --> src/lib.rs:238:67
      |
  238 |           if self.capabilities.contains(MemoryCapability::Internal) {
      |  ___________________________________________________________________^
  239 | |             defmt::write!(fmt, "Internal");
  240 | |         } else if self.capabilities.contains(MemoryCapability::External) {
      | |_________^
      |
  note: same as this
     --> src/lib.rs:240:74
      |
  240 |           } else if self.capabilities.contains(MemoryCapability::External) {
      |  __________________________________________________________________________^
  241 | |             defmt::write!(fmt, "External");
  242 | |         } else {
      | |_________^
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else
      = note: `-D clippy::if-same-then-else` implied by `-D warnings`
      = help: to override `-D warnings` add `#[allow(clippy::if_same_then_else)]`
  
  error: this `if` has identical blocks
  Error:    --> src/lib.rs:240:74
      |
  240 |           } else if self.capabilities.contains(MemoryCapability::External) {
      |  __________________________________________________________________________^
  241 | |             defmt::write!(fmt, "External");
  242 | |         } else {
      | |_________^
      |
  note: same as this
     --> src/lib.rs:242:16
      |
  242 |           } else {
      |  ________________^
  243 | |             defmt::write!(fmt, "Unknown");
  244 | |         }
      | |_________^
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else

I expected to see this happen:

Clippy should not emit a lint warning.

Version
clippy 0.1.94 (fecb335cba 2026-01-07)
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.