rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

cast-related lint should warn internal macros

Open taiki-e opened this issue 1 year ago • 1 comments

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

taiki-e avatar Oct 31 '23 12:10 taiki-e

this is because almost all cast-related lints has one from_expansion guard clause

should be an easy fix, @rustbot label +good-first-issue

J-ZhengLi avatar Feb 27 '24 09:02 J-ZhengLi

@rustbot claim

franciscoBSalgueiro avatar Apr 03 '24 16:04 franciscoBSalgueiro