#16356·rust-clippy

`if_same_then_else` triggers when bodies only differ in attributes

Author: bugadaniCreated Jan 8, 2026Updated Sep 18, 2026
LabelsC-bugI-false-positive

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:

rust
{
    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