-
Notifications
You must be signed in to change notification settings - Fork 1.9k
refactor: use type-based checks in arithmetic_side_effects
#16375
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?
refactor: use type-based checks in arithmetic_side_effects
#16375
Conversation
Instead of using string representation of types to compare them, use the compiler's type system directly via ty.kind() and opt_diag_name(). This approach: - Correctly handles type aliases (core vs std paths) - Is more efficient than string comparison - Follows the pattern used in disallowed_types lint The change affects has_allowed_binary() and has_allowed_unary() which now check: - Float types via ty::FloatTy::F32/F64 matching - String type via diagnostic item - Saturating/Wrapping via diagnostic items Falls back to string matching for user-configured types from clippy.toml to maintain backward compatibility. Fixes rust-lang#16359 changelog: [`arithmetic_side_effects`]: use type-based matching instead of strings
|
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.
It is a first step, but all string comparisons have to get away.
Also, can you please include examples where type aliases are used, both for primitive and composite types?
r? samueltardieu
@rustbot author
| let lhs_ty_string = lhs_ty.to_string(); | ||
| let lhs_ty_string_elem = lhs_ty_string.split('<').next().unwrap_or_default(); | ||
| let rhs_ty_string = rhs_ty.to_string(); | ||
| let rhs_ty_string_elem = rhs_ty_string.split('<').next().unwrap_or_default(); |
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.
Those to_string() and split() need to be gone as well, as well as the maps containing strings.
|
Reminder, once the PR becomes ready for a review, use |
|
Sure! working on it. |
|
I've completed the implementation for built-in type checks (String, Saturating, Wrapping) using is_diag_item, and added type alias tests. For user-configured types from clippy.toml, the current config allows simple names like Should I:
or some other approach that you'd recommend? |
The lint documentation is unclear indeed. Types are named from the top of the main module because they are compared against the output of Maybe What do you think? |
|
Thanks for the suggestion! I'll look into enhancing lookup_path() to also search from the current crate root. I'll implement it and see if it works for the existing test cases. |
|
Pushed! Enhanced Still have the |
…te-relative paths, add type alias tests
24e4352 to
76c4428
Compare
This PR refactors
arithmetic_side_effectsto use type-based checks instead of string representation matching, as suggested in #16359.Changes
Replaced string-based type comparison in
has_allowed_binary()andhas_allowed_unary()with proper type system checks:ty::FloatTy::F32 | ty::FloatTy::F64matching viaty.kind()opt_diag_name()to check forsym::Stringopt_diag_name()forsym::Saturating | sym::WrappingThis follows the pattern used in
disallowed_typeslint.Benefits
core::num::Wrapping==std::num::Wrapping)String-based matching is preserved as fallback for user-configured types from
clippy.toml.Testing
All
arithmetic_side_effectstests pass.changelog: [
arithmetic_side_effects]: use type-based matching instead of stringsFixes #16359