-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warn-by-default lint against pointer trait comparisons
- Loading branch information
Showing
5 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// check-pass | ||
|
||
struct A; | ||
struct B; | ||
|
||
trait T {} | ||
impl T for A {} | ||
impl T for B {} | ||
|
||
fn main() { | ||
let ab = (A, B); | ||
let a = &ab.0 as *const dyn T; | ||
let b = &ab.1 as *const dyn T; | ||
|
||
let _ = a == b; | ||
//~^ WARN incorrect pointer trait comparison | ||
let _ = a != b; | ||
//~^ WARN incorrect pointer trait comparison | ||
|
||
let _ = a as *const () == b as *const (); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
warning: incorrect pointer trait comparison, the comparison include metadata which is not guaranteed to be equivalent | ||
--> $DIR/pointer_trait_comparisons.rs:15:13 | ||
| | ||
LL | let _ = a == b; | ||
| ^^^^^^ | ||
| | ||
= note: `#[warn(invalid_pointer_trait_comparisons)]` on by default | ||
help: use explicit `std::ptr::eq` method to compare metadata and addresses | ||
| | ||
LL | let _ = std::ptr::eq(a, b); | ||
| +++++++++++++ ~ + | ||
help: convert to un-typed pointers to only compare their addresses | ||
| | ||
LL | let _ = a as *const () == b as *const (); | ||
| ++++++++++++ ++++++++++++ | ||
|
||
warning: incorrect pointer trait comparison, the comparison include metadata which is not guaranteed to be equivalent | ||
--> $DIR/pointer_trait_comparisons.rs:17:13 | ||
| | ||
LL | let _ = a != b; | ||
| ^^^^^^ | ||
| | ||
help: use explicit `std::ptr::eq` method to compare metadata and addresses | ||
| | ||
LL | let _ = !std::ptr::eq(a, b); | ||
| ++++++++++++++ ~ + | ||
help: convert to un-typed pointers to only compare their addresses | ||
| | ||
LL | let _ = a as *const () != b as *const (); | ||
| ++++++++++++ ++++++++++++ | ||
|
||
warning: 2 warnings emitted | ||
|