Skip to content

cast-related lint should warn internal macros #11738

@taiki-e

Description

@taiki-e

Summary

It would be reasonable not to warn about code generated by external macros (macros defined in dependencies or standard libraries), since there is no way for the user to fix it. However, it would make sense to warn about code generated by internal macros (macros defined in the current crate).

I have confirmed that at least the following lints have this false negative (playground):

  • cast_lossless
  • cast_possible_truncation
  • cast_precision_loss
  • cast_sign_loss

And I have confirmed that at least the following lint does NOT have this false negative (playground):

  • as_conversions

Lint Name

cast_lossless,cast_possible_truncation,cast_precision_loss,cast_sign_loss

Reproducer

I tried this code:

#![warn(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)]

macro_rules! m {
    () => {
        let _ = i32::MIN as u32; // cast_sign_loss
        let _ = u32::MAX as u8; // cast_possible_truncation
        let _ = std::f64::consts::PI as f32; // cast_possible_truncation
        let _ = 0i8 as i32; // cast_lossless
    };
}

fn main() {
    m!();
}

I expected to see this happen: clippy warns 4 casts inside macros

Instead, this happened: no warnings

playground

Version

rustc 1.75.0-nightly (31bc7e2c4 2023-10-30)
binary: rustc
commit-hash: 31bc7e2c47e82798a392c770611975a6883132c8
commit-date: 2023-10-30
host: aarch64-apple-darwin
release: 1.75.0-nightly
LLVM version: 17.0.3

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions