You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Move the trivial_numeric_casts lint to allow-by-default #1020
This lint was recently added in rust-lang/rust#23630 as part of #803 and it disallows casts such as foo as i64 when foo has the type i64.
In theory this does sound like a lint which is nice to have, but I've found that in the context of FFI it's doesn't always play out so well. Because libc::c_int is simply an alias of some concrete primitive type, it is typically casted out into a known integer to be worked with in Rust. In this specific case the target type of i32 is frequently chosen, triggering this lint. Having to annotate crates with #[allow(trivial_numeric_casts)] is somewhat annoying for this common use case, and the broader lint (trivial_casts) seems more relevant to remain as warn-by-default.
I do not oppose this change. I think we ended up with warn by default by miscommunication, rather than intention.
I marginally prefer warn by default because I think that ought to be the default behaviour for most integer casts, but I think I have a much higher tolerance for allows in my code than others and I see the merit of allow by default for the sake of FFI.
At the risk of sounding repetitive, what about an attribute (maybe #[allow(trivial_numeric_casts)] itself, but that might not work) on the types aliases which may vary between compilations?
I would much prefer to have the lint simply not warn if casting between type aliases. Warning me by default when I say something like 42i32 as i32 is reasonable; warning me by default when I say something like 42i32 as c_int is not. If we make this lint allow-by-default I'm not sure who would ever actually turn it on.
Note that this isn't just an issue with type aliases that may vary by configuration, it's also an issue with type aliases where the programmer simply doesn't necessarily know which concrete type the type alias is an alias for (and isn't required to know either; if they were then why would I be using a type alias?) or does know but simply wants to make it clear to any potential readers that they're intentionally using the type alias because it carries some semantic meaning.
Activity
nrc commentedon Mar 26, 2015
I do not oppose this change. I think we ended up with warn by default by miscommunication, rather than intention.
I marginally prefer warn by default because I think that ought to be the default behaviour for most integer casts, but I think I have a much higher tolerance for
allows in my code than others and I see the merit of allow by default for the sake of FFI.sfackler commentedon Mar 26, 2015
See also rust-lang/rust#23734
mahkoh commentedon Mar 27, 2015
If only someone had mentioned this exact problem during the RFC discussion.
eddyb commentedon Mar 27, 2015
At the risk of sounding repetitive, what about an attribute (maybe
#[allow(trivial_numeric_casts)]itself, but that might not work) on the types aliases which may vary between compilations?lilyball commentedon Mar 27, 2015
I would much prefer to have the lint simply not warn if casting between type aliases. Warning me by default when I say something like
42i32 as i32is reasonable; warning me by default when I say something like42i32 as c_intis not. If we make this lint allow-by-default I'm not sure who would ever actually turn it on.Note that this isn't just an issue with type aliases that may vary by configuration, it's also an issue with type aliases where the programmer simply doesn't necessarily know which concrete type the type alias is an alias for (and isn't required to know either; if they were then why would I be using a type alias?) or does know but simply wants to make it clear to any potential readers that they're intentionally using the type alias because it carries some semantic meaning.
scottmcm commentedon Aug 27, 2017
This appears to have happened: https://play.rust-lang.org/?gist=227c8025f371351c94cc404a57765c23&version=stable