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

Already on GitHub? Sign in to your account

ptr_as_ptr does not trigger inside macros #8120

Closed
lopopolo opened this issue Dec 13, 2021 · 2 comments
Closed

ptr_as_ptr does not trigger inside macros #8120

lopopolo opened this issue Dec 13, 2021 · 2 comments
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

@lopopolo
Copy link

lopopolo commented Dec 13, 2021

Summary

Casting a pointer with as does not trigger for code that comes from macro expansion even if that macro is local to the crate.

Lint Name

ptr_as_ptr

Reproducer

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=62c639bef9acba6c027e0e027999e78e

#![warn(clippy::all)]
#![warn(clippy::pedantic)]

extern "C" fn foo(ch: *const i8) {
    let _ = ch;
    todo!();
}

const F: &[u8] = b"x\0";

macro_rules! doit {
    () => {
        foo(F.as_ptr() as *const i8)
    }
}

pub fn main() {
    foo(F.as_ptr() as *const i8);
    doit!{}
}

I expected to see this happen: warning on the function call and on the function call inside the macro.

Instead, this happened: only one warning was issued:

    Checking playground v0.0.1 (/playground)
warning: `as` casting between raw pointers without changing its mutability
  --> src/main.rs:18:9
   |
18 |     foo(F.as_ptr() as *const i8);
   |         ^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `F.as_ptr().cast::<i8>()`
   |
note: the lint level is defined here
  --> src/main.rs:2:9
   |
2  | #![warn(clippy::pedantic)]
   |         ^^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::ptr_as_ptr)]` implied by `#[warn(clippy::pedantic)]`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 1.06s

Version

Rust 1.57.0 stable on playground
@lopopolo lopopolo 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 Dec 13, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Dec 14, 2021
@rsmantini
Copy link
Contributor

Hey, I would like to have go on this one.
@rustbot claim

bors added a commit that referenced this issue Feb 17, 2022
trigger  `ptr_as_ptr` inside macros

This PR makes `ptr_as_ptr` trigger inside macros

Fixes issue #8120

changelog: ``[`ptr_as_ptr`]`` is now triggered inside macros

r? `@llogiq`
@giraffate
Copy link
Contributor

#8442 was merged, so I close this.

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

No branches or pull requests

3 participants