-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Take lifetime extension into account in ref_as_ptr
#12260
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,61 @@ | ||
#![warn(clippy::ref_as_ptr)] | ||
#![allow(clippy::unnecessary_mut_passed)] | ||
|
||
fn f<T>(_: T) {} | ||
|
||
fn main() { | ||
let _ = &1u8 as *const _; | ||
let _ = &2u32 as *const u32; | ||
let _ = &3.0f64 as *const f64; | ||
f(&1u8 as *const _); | ||
f(&2u32 as *const u32); | ||
f(&3.0f64 as *const f64); | ||
|
||
let _ = &4 as *const _ as *const f32; | ||
let _ = &5.0f32 as *const f32 as *const u32; | ||
f(&4 as *const _ as *const f32); | ||
f(&5.0f32 as *const f32 as *const u32); | ||
|
||
let _ = &mut 6u8 as *const _; | ||
let _ = &mut 7u32 as *const u32; | ||
let _ = &mut 8.0f64 as *const f64; | ||
f(&mut 6u8 as *const _); | ||
f(&mut 7u32 as *const u32); | ||
f(&mut 8.0f64 as *const f64); | ||
|
||
let _ = &mut 9 as *const _ as *const f32; | ||
let _ = &mut 10.0f32 as *const f32 as *const u32; | ||
f(&mut 9 as *const _ as *const f32); | ||
f(&mut 10.0f32 as *const f32 as *const u32); | ||
|
||
let _ = &mut 11u8 as *mut _; | ||
let _ = &mut 12u32 as *mut u32; | ||
let _ = &mut 13.0f64 as *mut f64; | ||
f(&mut 11u8 as *mut _); | ||
f(&mut 12u32 as *mut u32); | ||
f(&mut 13.0f64 as *mut f64); | ||
|
||
let _ = &mut 14 as *mut _ as *const f32; | ||
let _ = &mut 15.0f32 as *mut f32 as *const u32; | ||
f(&mut 14 as *mut _ as *const f32); | ||
f(&mut 15.0f32 as *mut f32 as *const u32); | ||
|
||
let _ = &1u8 as *const _; | ||
let _ = &2u32 as *const u32; | ||
let _ = &3.0f64 as *const f64; | ||
f(&1u8 as *const _); | ||
f(&2u32 as *const u32); | ||
f(&3.0f64 as *const f64); | ||
|
||
let _ = &4 as *const _ as *const f32; | ||
let _ = &5.0f32 as *const f32 as *const u32; | ||
f(&4 as *const _ as *const f32); | ||
f(&5.0f32 as *const f32 as *const u32); | ||
|
||
let val = 1; | ||
let _ = &val as *const _; | ||
let _ = &val as *const i32; | ||
f(&val as *const _); | ||
f(&val as *const i32); | ||
|
||
let _ = &val as *const _ as *const f32; | ||
let _ = &val as *const i32 as *const f64; | ||
f(&val as *const _ as *const f32); | ||
f(&val as *const i32 as *const f64); | ||
|
||
let mut val: u8 = 2; | ||
let _ = &mut val as *mut u8; | ||
let _ = &mut val as *mut _; | ||
f(&mut val as *mut u8); | ||
f(&mut val as *mut _); | ||
|
||
f(&mut val as *const u8); | ||
f(&mut val as *const _); | ||
|
||
let _ = &mut val as *const u8; | ||
let _ = &mut val as *const _; | ||
f(&mut val as *const u8 as *const f64); | ||
f::<*const Option<u8>>(&mut val as *const _ as *const _); | ||
|
||
let _ = &mut val as *const u8 as *const f64; | ||
let _: *const Option<u8> = &mut val as *const _ as *const _; | ||
f(&std::array::from_fn(|i| i * i) as *const [usize; 7]); | ||
f(&mut std::array::from_fn(|i| i * i) as *const [usize; 8]); | ||
f(&mut std::array::from_fn(|i| i * i) as *mut [usize; 9]); | ||
|
||
let _ = &std::array::from_fn(|i| i * i) as *const [usize; 7]; | ||
let _ = &mut std::array::from_fn(|i| i * i) as *const [usize; 8]; | ||
let _ = &mut std::array::from_fn(|i| i * i) as *mut [usize; 9]; | ||
let _ = &String::new() as *const _; | ||
let _ = &mut String::new() as *mut _; | ||
const FOO: *const String = &String::new() as *const _; | ||
} | ||
|
||
#[clippy::msrv = "1.75"] | ||
|
@@ -58,27 +64,27 @@ fn _msrv_1_75() { | |
let mut_val = &mut 42_i32; | ||
|
||
// `std::ptr::from_{ref, mut}` was stabilized in 1.76. Do not lint this | ||
let _ = val as *const i32; | ||
let _ = mut_val as *mut i32; | ||
f(val as *const i32); | ||
f(mut_val as *mut i32); | ||
} | ||
|
||
#[clippy::msrv = "1.76"] | ||
fn _msrv_1_76() { | ||
let val = &42_i32; | ||
let mut_val = &mut 42_i32; | ||
|
||
let _ = val as *const i32; | ||
let _ = mut_val as *mut i32; | ||
f(val as *const i32); | ||
f(mut_val as *mut i32); | ||
} | ||
|
||
fn foo(val: &[u8]) { | ||
let _ = val as *const _; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, |
||
let _ = val as *const [u8]; | ||
f(val as *const _); | ||
f(val as *const [u8]); | ||
} | ||
|
||
fn bar(val: &mut str) { | ||
let _ = val as *mut _; | ||
let _ = val as *mut str; | ||
f(val as *mut _); | ||
f(val as *mut str); | ||
} | ||
|
||
struct X<'a>(&'a i32); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't this change go way too far?
let _ = mut_val as *mut i32;
should absolutely trigger the lint, no? The issue isn't the temporariness of the resultant pointer, but whether the expression being cast is a&x
or&mut x
expression, right?