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
A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-perfLint: Belongs in the perf lint groupL-styleLint: Belongs in the style lint group
When a user implements a trait, where the function signature returns a result (like in FromStr or TryFrom) and the function body doesn't return an error, the lint should suggest to use Infallible or ! (! isn't stabilized yet, rust-lang/rust#35121).
What it does
Checks for trait implementations, where the function signature returns a Result<T, E> and the body doesn't return the error-variant.
Why is this bad
Using ! or an enum like Infallible , that can not be constructed, allows the compiler to better optimize the code (similar to unreachable!).
Example
use std::str::FromStr;structExampleStruct(String);implFromStrforExampleStruct{typeErr = Box<dyn std::error::Error>;fnfrom_str(input:&str) -> Result<Self,Self::Err>{Ok(Self(input.to_string()))}}
better
use std::str::FromStr;structExampleStruct(String);implFromStrforExampleStruct{typeErr = std::convert::Infallible;fnfrom_str(input:&str) -> Result<Self,Self::Err>{Ok(Self(input.to_string()))}}
but some of them might be intentional, to not break future versions.
In the above example it might be better to drop the Result type, which cannot be done for trait implementations. (hence the lint)
The text was updated successfully, but these errors were encountered:
flip1995
added
L-perf
Lint: Belongs in the perf lint group
L-style
Lint: Belongs in the style lint group
E-medium
Call for participation: Medium difficulty level problem and requires some initial experience.
A-lint
Area: New lints
labels
Oct 9, 2019
A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-perfLint: Belongs in the perf lint groupL-styleLint: Belongs in the style lint group
When a user implements a trait, where the function signature returns a result (like in
FromStr
orTryFrom
) and the function body doesn't return an error, the lint should suggest to useInfallible
or!
(!
isn't stabilized yet, rust-lang/rust#35121).What it does
Checks for trait implementations, where the function signature returns a
Result<T, E>
and the body doesn't return the error-variant.Why is this bad
Using
!
or an enum likeInfallible
, that can not be constructed, allows the compiler to better optimize the code (similar tounreachable!
).Example
better
even better (
!
isn't stabilized yet)This lint could be generalized to lint against all kind of functions like
but some of them might be intentional, to not break future versions.
In the above example it might be better to drop the
Result
type, which cannot be done for trait implementations. (hence the lint)The text was updated successfully, but these errors were encountered: