Skip to content

[refurb] Mark int and bool cases for Decimal.from_float as safe fixes in FURB164 tests#19468

Merged
ntBre merged 3 commits into
astral-sh:mainfrom
danparizher:fix-19460
Jul 28, 2025
Merged

[refurb] Mark int and bool cases for Decimal.from_float as safe fixes in FURB164 tests#19468
ntBre merged 3 commits into
astral-sh:mainfrom
danparizher:fix-19460

Conversation

@danparizher
Copy link
Copy Markdown
Contributor

Summary

Fixes #19460

}
replacement_arg = chars.into_iter().collect();
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for float("\x2dnan") or float("\N{HYPHEN-MINUS}nan")? Cf. #16771.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it covers both of those cases

Copy link
Copy Markdown
Contributor

@ntBre ntBre Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add tests for those cases? I also think we could use the return value of as_non_finite_float_string_literal just above this instead of doing some of the manual manipulation ourselves. That should normalize the result so that we can just compare against "-nan" directly.

@ntBre ntBre self-requested a review July 24, 2025 13:07
@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Jul 24, 2025
Copy link
Copy Markdown
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just a couple of suggestions

)
})
.unwrap_or_default()
.unwrap_or((false, false))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be equivalent, but it's probably more readable, even though I think I suggested the old version 😆

}
replacement_arg = chars.into_iter().collect();
}
}
Copy link
Copy Markdown
Contributor

@ntBre ntBre Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add tests for those cases? I also think we could use the return value of as_non_finite_float_string_literal just above this instead of doing some of the manual manipulation ourselves. That should normalize the result so that we can just compare against "-nan" directly.


match (method_name, constructor) {
// Decimal.from_float accepts int, bool, float
// Decimal.from_float: Only int or bool are safe (float is unsafe due to FloatOperation trap)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to update the name of this function and also the comment where it's called. is_valid made me think we would bail out without a diagnostic or without a fix if the result is invalid, but it's really just checking the fix safety.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jul 24, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@danparizher danparizher requested a review from ntBre July 24, 2025 21:13
Copy link
Copy Markdown
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just one more tiny nit that I'll probably just apply and then merge this.

Comment thread crates/ruff_linter/src/rules/refurb/rules/unnecessary_from_float.rs Outdated
@ntBre ntBre enabled auto-merge (squash) July 28, 2025 14:18
@ntBre ntBre merged commit 201b079 into astral-sh:main Jul 28, 2025
34 checks passed
@danparizher danparizher deleted the fix-19460 branch July 28, 2025 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB164 fix has two problems related to Decimal.from_float

3 participants