rust-lang / rust-lang/rust-clippy

cargo clippy --fix disrespects `#[cfg()]` attr

Open
#10,169 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Curiously I can not reproduce it with an entirely minimal Foo struct, but here at least in this very minimal code, running cargo clippy --fix will cause the manual implementation of Default to be replaced with a derive, causing the #[cfg()] attribute above it to be erroneously applied to the next block of code. Causing my code to break

Reproducer
#[derive(Debug, Clone)]
pub struct CRect {
    pub page_number: usize,
    pub top: f32,
    pub left: f32,
    pub width: f32,
    pub height: f32,
}

#[cfg(any(test, feature = "test"))]
impl Default for CRect {
    fn default() -> Self {
        Self {
            page_number: Default::default(),
            top: Default::default(),
            left: Default::default(),
            width: Default::default(),
            height: Default::default(),
        }
    }
}

#[inline]
pub fn approx_f32(f: f32) -> u16 {
    (f * 1000.0).round() as u16
}

#[test]
fn test() {
    CRect::default();
}
Version
rustc 1.66.0-beta.1 (e080cc5a6 2022-11-01)
binary: rustc
commit-hash: e080cc5a659fb760c0bc561b722a790dad35b5e1
commit-date: 2022-11-01
host: x86_64-unknown-linux-gnu
release: 1.66.0-beta.1
LLVM version: 15.0.2
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.

Research direction

Start by running the provided reproducer with the listed rustc 1.66.0-beta.1 toolchain and cargo clippy --fix. Trace the lint's suggested replacement for the manual Default implementation and how the #[cfg()] attribute is retained or moved. Done means the fix no longer applies the attribute to the following block and the reproducer continues to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.