Inspired by a tangent in another thread, I think we should add an attribute to indicate that a function purely edits values in-place without other side-effects.
These two functions feel very similar to me:
pub fn foo1() {
let mut foo = 7;
println!("{foo}");
foo += 1;
}
pub fn foo2() {
let mut foo = 7;
println!("{foo}");
inc_one(&mut foo);
}
fn inc_one(n: &mut i32) {
*n += 1;
}
But foo1
raises a lint for the foo += 1
assignment being unused, whereas foo2
does the same thing but raises no such lint, since rustc can't see across both functions to conclude that there's an issue.
I think it'd be useful to add an attribute #[pure_inplace_edit]
(feel free to bikeshed the name) to functions to indicate that the function should be treated as an inplace edit of any &mut _
arguments for the purposes of the lint. This attribute doesn't change the behavior of the code, only whether rustc raises this lint. You could think of this attribute as the equivalent to #[must_use]
, but for &mut
arguments instead of the return value.
So we change the above to:
#[pure_inplace_edit]
fn inc_one(n: &mut i32) {
*n += 1;
}
And now rustc
will see that inc_one(&mut foo);
edits foo
in-place the same as foo += 1;
does, and raise the same lint.