Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive on unused_import and dead_code #12418

Closed
shnewto opened this issue Mar 4, 2024 · 3 comments
Closed

False positive on unused_import and dead_code #12418

shnewto opened this issue Mar 4, 2024 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@shnewto
Copy link
Contributor

shnewto commented Mar 4, 2024

Summary

hello! I love clippy, don't know what I'd do without it 馃槃 but I caught a couple false positives I'd like to share. thanks for taking a look!

reproducer

link to source effected https://github.com/shnewto/edges/blob/7e2566e73a6fd0bafc4756e07293d5264556391d/src/edges.rs#L244-L321

here it is too, notice the #[allow(dead_code)] and #[allow(unused_imports)]

impl fmt::Debug for Edges {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        #[derive(Debug)]
        #[allow(dead_code)]
        struct EdgesDisplay {
            raw: Vec<Vec<Vec2>>,
            translated: Vec<Vec<Vec2>>,
        }


        let edges_display = EdgesDisplay {
            raw: self.image_edges(false),
            translated: self.image_edges(false),
        };
        write!(f, "{:#?}", edges_display)
    }
}


#[cfg(feature = "bevy")]
mod tests {
    #[allow(unused_imports)]
    use super::Edges;
    #[allow(unused_imports)]
    use bevy::{prelude::Image, render::texture::ImageType};
    #[allow(unused_imports)]
    use std::path::Path;


    #[test]
    fn same_image_same_edges() {
        let dynamic_image = image::open(Path::new("assets/car.png")).unwrap();
        let dynamic_edges = Edges::from(dynamic_image);


        let bevy_image = Image::from_buffer(
            include_bytes!("../assets/car.png"), // buffer
            ImageType::Extension("png"),
            Default::default(),
            true, //
            Default::default(),
            Default::default(),
        )
        .unwrap();
        let bevy_edges = Edges::from(bevy_image);


        assert_eq!(
            dynamic_edges.single_image_edge_raw(),
            bevy_edges.single_image_edge_raw()
        );
        assert_eq!(
            dynamic_edges.single_image_edge_translated(),
            bevy_edges.single_image_edge_translated()
        );
    }


    #[test]
    fn same_images_same_edges() {
        let dynamic_image = image::open(Path::new("assets/boulders.png")).unwrap();
        let dynamic_edges = Edges::from(dynamic_image);


        let bevy_image = Image::from_buffer(
            include_bytes!("../assets/boulders.png"), // buffer
            ImageType::Extension("png"),
            Default::default(),
            true, //
            Default::default(),
            Default::default(),
        )
        .unwrap();
        let bevy_edges = Edges::from(bevy_image);


        assert_eq!(
            dynamic_edges.multi_image_edges_raw(),
            bevy_edges.multi_image_edges_raw()
        );
        assert_eq!(
            dynamic_edges.multi_image_edge_translated(),
            bevy_edges.multi_image_edge_translated()
        );
    }
}

version

rustup --version

rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.78.0-nightly (516b6162a 2024-03-03)`

rustc -Vv

rustc 1.78.0-nightly (516b6162a 2024-03-03)
binary: rustc
commit-hash: 516b6162a2ea8e66678c09e8243ebd83e4b8eeea
commit-date: 2024-03-03
host: x86_64-apple-darwin
release: 1.78.0-nightly
LLVM version: 18.1.0

output

clippy output after cargo clippy --all-features --all-targets (and just running cargo clippy without args)

warning: unused import: `super::Edges`
   --> src/edges.rs:262:9
    |
262 |     use super::Edges;
    |         ^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: unused imports: `prelude::Image`, `render::texture::ImageType`
   --> src/edges.rs:263:16
    |
263 |     use bevy::{prelude::Image, render::texture::ImageType};
    |                ^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused import: `std::path::Path`
   --> src/edges.rs:264:9
    |
264 |     use std::path::Path;
    |         ^^^^^^^^^^^^^^^

warning: fields `raw` and `translated` are never read
   --> src/edges.rs:248:13
    |
247 |         struct EdgesDisplay {
    |                ------------ fields in this struct
248 |             raw: Vec<Vec<Vec2>>,
    |             ^^^
249 |             translated: Vec<Vec<Vec2>>,
    |             ^^^^^^^^^^
    |
    = note: `EdgesDisplay` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
    = note: `#[warn(dead_code)]` on by default

warning: `edges` (lib) generated 4 warnings (run `cargo clippy --fix --lib -p edges` to apply 3 suggestions)
warning: `edges` (lib test) generated 1 warning (1 duplicate)

compile errors after taking clippy's suggestion in the mod and running cargo build --features bevy

error[E0560]: struct `EdgesDisplay` has no field named `raw`
   --> src/edges.rs:251:13
    |
251 |             raw: self.image_edges(false),
    |             ^^^ `EdgesDisplay` does not have this field
    |
    = note: all struct fields are already assigned

error[E0560]: struct `EdgesDisplay` has no field named `translated`
   --> src/edges.rs:252:13
    |
252 |             translated: self.image_edges(false),
    |             ^^^^^^^^^^ `EdgesDisplay` does not have this field
    |
    = note: all struct fields are already assigned

For more information about this error, try `rustc --explain E0560`.
error: could not compile `edges` (lib) due to 2 previous errors

compile errors after taking clippy's suggestion in the tests and running cargo test --features bevy

error[E0433]: failed to resolve: use of undeclared type `Path`
   --> src/edges.rs:265:41
    |
265 |         let dynamic_image = image::open(Path::new("assets/car.png")).unwrap();
    |                                         ^^^^ use of undeclared type `Path`
    |
help: consider importing this struct
    |
263 +     use std::path::Path;
    |

error[E0433]: failed to resolve: use of undeclared type `Edges`
   --> src/edges.rs:266:29
    |
266 |         let dynamic_edges = Edges::from(dynamic_image);
    |                             ^^^^^ use of undeclared type `Edges`
    |
help: consider importing one of these items
    |
263 +     use bevy::ecs::archetype::Edges;
    |
263 +     use bevy::render::render_graph::Edges;
    |
263 +     use crate::Edges;
    |

error[E0433]: failed to resolve: use of undeclared type `Image`
   --> src/edges.rs:268:26
    |
268 |         let bevy_image = Image::from_buffer(
    |                          ^^^^^ use of undeclared type `Image`
    |
help: consider importing this struct
    |
263 +     use bevy::render::texture::Image;
    |

error[E0433]: failed to resolve: use of undeclared type `ImageType`
   --> src/edges.rs:270:13
    |
270 |             ImageType::Extension("png"),
    |             ^^^^^^^^^ use of undeclared type `ImageType`
    |
help: consider importing this enum
    |
263 +     use bevy::render::texture::ImageType;
    |

error[E0433]: failed to resolve: use of undeclared type `Edges`
   --> src/edges.rs:277:26
    |
277 |         let bevy_edges = Edges::from(bevy_image);
    |                          ^^^^^ use of undeclared type `Edges`
    |
help: consider importing one of these items
    |
263 +     use bevy::ecs::archetype::Edges;
    |
263 +     use bevy::render::render_graph::Edges;
    |
263 +     use crate::Edges;
    |

error[E0433]: failed to resolve: use of undeclared type `Path`
   --> src/edges.rs:291:41
    |
291 |         let dynamic_image = image::open(Path::new("assets/boulders.png")).unwrap();
    |                                         ^^^^ use of undeclared type `Path`
    |
help: consider importing this struct
    |
263 +     use std::path::Path;
    |

error[E0433]: failed to resolve: use of undeclared type `Edges`
   --> src/edges.rs:292:29
    |
292 |         let dynamic_edges = Edges::from(dynamic_image);
    |                             ^^^^^ use of undeclared type `Edges`
    |
help: consider importing one of these items
    |
263 +     use bevy::ecs::archetype::Edges;
    |
263 +     use bevy::render::render_graph::Edges;
    |
263 +     use crate::Edges;
    |

error[E0433]: failed to resolve: use of undeclared type `Image`
   --> src/edges.rs:294:26
    |
294 |         let bevy_image = Image::from_buffer(
    |                          ^^^^^ use of undeclared type `Image`
    |
help: consider importing this struct
    |
263 +     use bevy::render::texture::Image;
    |

error[E0433]: failed to resolve: use of undeclared type `ImageType`
   --> src/edges.rs:296:13
    |
296 |             ImageType::Extension("png"),
    |             ^^^^^^^^^ use of undeclared type `ImageType`
    |
help: consider importing this enum
    |
263 +     use bevy::render::texture::ImageType;
    |

error[E0433]: failed to resolve: use of undeclared type `Edges`
   --> src/edges.rs:303:26
    |
303 |         let bevy_edges = Edges::from(bevy_image);
    |                          ^^^^^ use of undeclared type `Edges`
    |
help: consider importing one of these items
    |
263 +     use bevy::ecs::archetype::Edges;
    |
263 +     use bevy::render::render_graph::Edges;
    |
263 +     use crate::Edges;
    |

For more information about this error, try `rustc --explain E0433`.

Again, thanks for taking a look. And thanks for all your incredible work!

Lint Name

unused_imports, dead_code

@shnewto shnewto added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 4, 2024
@shnewto
Copy link
Contributor Author

shnewto commented Mar 4, 2024

well okay, the mod test unused imports is not the false positive I thought! I was missing #[cfg(test)] over mod test, i.e.

#[cfg(feature = "bevy")]
#[cfg(test)]
mod tests {

that solves that one, but the dead_code lint in the Debug trait i think is still a false positive

@shnewto
Copy link
Contributor Author

shnewto commented Mar 4, 2024

another note, it was suggested to me that clippy is probably right regarding the struct fields in the Debug trait too, that writing/printing the struct doesn't qualify as reading the fields? I'm okay with that I think 馃

both of these scenarios though do feel like they require more technical rust knowledge to understand than I'm used to needing with clippy.

and if nothing else running cargo clippy --fix --all-features --all-targets breaks working code 馃槄

@Alexendoo
Copy link
Member

These are lints from rustc rather than Clippy, Debug being excluded from dead code analysis is intentional so it may stay that #[allow(dead_code)] is required, but there's a rustc issue here rust-lang/rust#123068

@Alexendoo Alexendoo closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants