Skip to content

cast-related lint should warn internal macros #11738

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
taiki-e opened this issue Oct 31, 2023 · 2 comments · Fixed by #12631
Closed

cast-related lint should warn internal macros #11738

taiki-e opened this issue Oct 31, 2023 · 2 comments · Fixed by #12631
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@taiki-e
Copy link
Member

taiki-e commented Oct 31, 2023

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 taiki-e added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Oct 31, 2023
@J-ZhengLi
Copy link
Member

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

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

@rustbot rustbot added the good-first-issue These issues are a good way to get started with Clippy label Feb 27, 2024
@franciscoBSalgueiro
Copy link
Contributor

@rustbot claim

bors added a commit that referenced this issue Apr 4, 2024
Allow `cast` lints in macros

closes: #11738

Removed the `from_expansion` guard clause for cast lints, so that these warnings can be generated for internal macros.

changelog: allow `cast` lints in macros
@bors bors closed this as completed in ac225a3 Apr 4, 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 good-first-issue These issues are a good way to get started with Clippy I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants