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

Trivial cast warning for cast that can't be removed #23742

Closed
bluss opened this issue Mar 26, 2015 · 5 comments
Closed

Trivial cast warning for cast that can't be removed #23742

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

Comments

@bluss
Copy link
Member

bluss commented Mar 26, 2015

The following code yields warnings:

warning: trivial cast: `&mut T` as `*mut T`, #[warn(trivial_casts)] on by default

Warning spots are marked // WARN below

#[warn(trivial_casts)]
enum Pair<T> {
    Both(T, T),
    One(T),
    None,
}

fn index_twice<T>(slc: &mut [T], a: usize, b: usize) -> Pair<&mut T>
{
    if a == b {
        slc.get_mut(a).map_or(Pair::None, Pair::One)
    } else {
        if a >= slc.len() || b >= slc.len() {
            Pair::None
        } else {
            // safe because a, b are in bounds and distinct
            unsafe {
                let ar = &mut *(slc.get_unchecked_mut(a) as *mut _); // WARN
                let br = &mut *(slc.get_unchecked_mut(b) as *mut _); // WARN
                Pair::Both(ar, br)
            }
        }
    }
}

The casts are non-trivial because without them the code does not pass the borrow check. Under the circumstances above we do so soundly by using an unsafe block.

Warning introduced in #23630

@bluss
Copy link
Member Author

bluss commented Mar 26, 2015

Note: I've got another instance of this where I'm not doing it in a pair, but for a single &mut, so it's not just the pairing that's the pattern here.

@petrochenkov
Copy link
Contributor

"Trivial" doesn't mean "can be omitted", the warning triggers because type ascription is supposed to be used here and not as. The problem is that type ascription is not implemented yet. I guess temporary #[allow(trivial_casts)] will be the correct solution in this situation.

@steveklabnik steveklabnik added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 27, 2015
@Mark-Simulacrum
Copy link
Member

As far as I can tell, this isn't really something that we can fix; it's working as intended. Replacing the single-line casts with something like the following resolves both the lint and doesn't introduce any errors. I wasn't quite able to get it working with type ascription, though, but I feel like that's likely my fault.

Should this be closed?

let ar: *mut _ = slc.get_unchecked_mut(a);
let ar = &mut *(ar);

@Mark-Simulacrum
Copy link
Member

Closing, please reopen if this is still an issue.

@bluss
Copy link
Member Author

bluss commented Apr 29, 2017

Trivial cast warnings are no longer enabled by default, so the issue is fixed that way.

tmandry added a commit to tmandry/rust that referenced this issue Sep 10, 2020
…_lint_doc, r=steveklabnik

Reword `trivial_casts` lint in rustc book to better explain what it does.

The current description of the trivial casts lint under the "allowed
by default" listing in the rustc book indicates the lint is for casts
which may be removed, which is less clear than saying it's for casts
which may be replaced by coercion (which is the wording used by the
error message included in the doc).

This commit changes the wording slightly to better describe what the
lint does.

This issue bit me in some recent code where I was attempting to
convert a `Vec<SomeType>` to a `Vec<SomeTraitObject>`, and
hit my project-wide `#![deny(trivial_casts)]` with
`map(|o| Box::new(o) as TraitObject)`. I'd read the book docs for
`trivial_casts` and was surprised by the error, as I took it to mean
the cast ought to be removed (rather than replaced by ascription
in this case). Removing the cast meant other code didn't compile,
and I then found issues like rust-lang#23742 and realized my misunderstanding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants