-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[refurb
] Handle non-finite decimals in verbose-decimal-constructor (FURB157)
#14596
base: main
Are you sure you want to change the base?
Conversation
|
crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs
Show resolved
Hide resolved
| "-infinity" | ||
| "nan" | ||
| "+nan" | ||
| "-nan" | ||
) { | ||
return; | ||
} |
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.
Unrelated to your fix: To avoid an allocation in many cases, it might be worth trying an exact match first before calling to_lowercase
.
let trimmed = float.value.to_str().trim();
if is_inf_infinity_or_nan(trimmed) {
return;
}
let normalized_float_string = trimmed.to_lowercase();
if is_inf_infinity_or_nan(&normalized_flaot_string) {
return;
}
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.
Maybe I'm misunderstanding but I think we have to allocate anyway: we only get to escape early here if float.value
is not inf, infinity, or nan (after trimming and lowercasing). So if we first checked that the trimmed version wasn't "nan" etc., we would still have to rule out the case where it's "NaN".
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.
Yes, it's just that we do it less often. I guess the overall idea is to spell out all common variations so that the rule can short-circuit for almost all valid-cases and only allocates for uncommon spellings (e.g. nAn
).
Co-authored-by: Micha Reiser <[email protected]>
This PR extends the Decimal parsing used in verbose-decimal-constructor (FURB157) to better handle non-finite
Decimal
objects, avoiding some false negatives.Closes #14587