Skip to content

Bogus trivial cast warning when comparing references cast to *const #23734

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
sfackler opened this issue Mar 26, 2015 · 3 comments
Closed

Bogus trivial cast warning when comparing references cast to *const #23734

sfackler opened this issue Mar 26, 2015 · 3 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@sfackler
Copy link
Member

fn main() {
    let a = &true;
    let b = &false;

    assert!(a as *const _ == b as *const _);
}
test.rs:5:30: 5:43 warning: trivial cast: `&bool` as `*const bool`, #[warn(trivial_casts)] on by default
test.rs:5     assert!(a as *const _ == b as *const _);
                                       ^~~~~~~~~~~~~
<std macros>:1:1: 5:46 note: in expansion of assert!
test.rs:5:5: 5:45 note: expansion site

But removing the second cast doesn't compile:

fn main() {
    let a = &true;
    let b = &false;

    assert!(a as *const _ == b);
}
test.rs:5:30: 5:31 error: mismatched types:
 expected `*const _`,
    found `&bool`
(expected *-ptr,
    found &-ptr) [E0308]
test.rs:5     assert!(a as *const _ == b);
                                       ^
<std macros>:1:1: 5:46 note: in expansion of assert!
test.rs:5:5: 5:33 note: expansion site
error: aborting due to previous error

cc @nrc

@sfackler sfackler added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 26, 2015
sfackler added a commit to sfackler/rust-postgres that referenced this issue Mar 26, 2015
Blocked on rust-lang/rust#23734 on the last
@nrc
Copy link
Member

nrc commented Mar 26, 2015

The lint runs when a cast can be replaced by a coercion, not when a cast can be removed. So, this is not bogus, although it is kind of annoying to fix without type ascription.

@nrc nrc closed this as completed Mar 26, 2015
@sfackler
Copy link
Member Author

Uh, so what's the resolution here? Should I just turn the lint off?

@nrc
Copy link
Member

nrc commented Mar 26, 2015

You could add a type annotation to the declaration of b so that it has the expected type, or introduce a temporary variable (b_as_raw or something). You could also turn the lint off until type ascription lands (hopefully soon) which will give you a nice way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

2 participants