-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add [manual_slice_size_calculation
]
#10601
Conversation
r? @llogiq (rustbot has picked a reviewer for you, use r? to override) |
b78ba71
to
b66aa09
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.
I'd like to see a test where either the size or length is bound to a temporary variable first. It's ok for now if that isn't linted, but sufficiently easy to change.
Otherwise the code looks good, and I'll gladly merge it with the additional test added.
fn simplify<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr1: &'tcx Expr<'tcx>, | ||
expr2: &'tcx Expr<'tcx>, | ||
) -> Option<&'tcx Expr<'tcx>> { | ||
simplify_half(cx, expr1, expr2).or_else(|| simplify_half(cx, expr2, expr1)) | ||
} |
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.
I swear I've seen this pattern multiple times. Perhaps we should make a generic clippy_utils
function of it.
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.
Such a helper could also fix some of the false negatives in my tests, e.g, size_of::<i32>() * 5 * slice.len()
. The helper's semantics could be:
Check if this expression is a chain of multiplications (or additions or something more general) where two (or
n
?) sub-expressions meet some condition.
I don't have the bandwidth to do it as part of this PR, though.
Done. I feel this kind of data flow analysis (where does a value come from, what do we know about it?) could be a whole new thing, applicable to a lot more than just this lint here. For example, in the following Java code Intellij warns that the List<String> strings = Collections.emptyList();
if (strings.isEmpty()) { // "Condition `strings.isEmpty()` is always `true`.
System.out.println("There are no strings");
} and in this code it warns that the list access will always fail: List<String> strings = foo();
if (strings.isEmpty()) {
System.out.println(strings.get(0)); // "The call to `get` always fails as an argument is out of bounds"
} Do we have anything like this in Clippy? |
We have, it's |
Thanks, I'll give it a shot. Let's not merge this yet. |
Done. Thanks for the suggestion, this is very nice! |
Great! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
…Denton Avoid some manual slice length calculation No need for us to write the multiplication when `size_of_val` does exactly what we need. (rust-lang/rust-clippy#10601)
Avoid some manual slice length calculation No need for us to write the multiplication when `size_of_val` does exactly what we need. (rust-lang/rust-clippy#10601)
Fixes: #10518
changelog: new lint [
manual_slice_size_calculation
]