-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[WIP] Trigger absurd_extreme_comparisons if Duration is less than zero #14817
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
base: master
Are you sure you want to change the base?
[WIP] Trigger absurd_extreme_comparisons if Duration is less than zero #14817
Conversation
118446e
to
e47e2fc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In its current state, the lint would:
- fail with a false positive in some situations where a non-zero
Duration
is returned by a function (see detailed comment) - fail with a false negative with other const initializers, such as
Duration::new(0, 0)
,Duration::from_secs_f32(0)
, andDuration::from_secs_f64(0)
.
This can be fixed in several ways:
- A list of methods to match against can be used, with a special case for
Duration::new(0, 0)
(easier) - Or the
const
evaluator could be enriched to include aDuration
variant (more complex, probably not worth it)
if let ExprKind::Call(_, args) = &expr.kind | ||
&& args.len() == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use something like:
if let ExprKind::Call(_, args) = &expr.kind | |
&& args.len() == 1 | |
if let ExprKind::Call(_, [arg]) = &expr.kind |
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Duration) | ||
&& let Some(Constant::Int(0)) = ConstEvalCtxt::new(cx).eval(&args[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not robust enough, as this will match any function whose argument is 0 and returns a Duration
, including a function such as:
fn one_second_plus(n: u64) -> Duration {
Duration::from_secs(n+1)
}
which does not return a zero duration.
e47e2fc
to
22ce6a7
Compare
22ce6a7
to
d6feb1f
Compare
I know the PR is still in draft mode, but I'll comment right now since function name matching has changed recently: we don't use But here, since you want to look up entities, there is also another way to go: you can add the path to the associated functions you need to identify, e.g., pub static DURATION_FROM_SECS: PathLookup = value_path!(core::time::Duration::from_secs); You will then be able to use Hope it helps. |
Close #14524
changelog: [
absurd_extreme_comparisons
]: triggers if Duration is less than 0