about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-04-23 21:50:54 +0200
committerGitHub <noreply@github.com>2019-04-23 21:50:54 +0200
commit2deae591a085183a56c550e06609b0c8cf157a33 (patch)
treec04f44cc7bde2f626bfc394772a5720ad850fed1 /src/test
parent62d1574876f5531bce1b267e62dff520d7adcbbb (diff)
parent23154db8371e5277c373578690b3a79d15e13180 (diff)
downloadrust-2deae591a085183a56c550e06609b0c8cf157a33.tar.gz
rust-2deae591a085183a56c550e06609b0c8cf157a33.zip
Rollup merge of #59839 - KodrAus:must-use-num, r=sfackler
Warn on unused results for operation methods on nums

From a suggestion by @llogiq

Adds a `#[must_use]` attribute to operation methods on integers that take self by value as the first operand and another value as the second. It makes it clear that these methods return the result of the operation instead of mutating `self`, which is the source of a rather embarrassing bug I had in a codebase of mine recently...

As an example:

```rust
struct Int {
   value: i64,
}

impl Int {
    fn add(&mut self, other: i64) {
        self.value.wrapping_add(other);
    }
}
```

Will produce a warning like:

```
warning: unused return value of `core::num::<impl i64>::wrapping_add` that must be used
 --> src/main.rs:7:7
  |
7 |       self.value.wrapping_add(other);
  |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_must_use)] on by default
  = note: this returns the result of the operation, without modifying the original
```

If this is something we're on board with, we could do something similar for `f32` and `f64` too. There are probably other methods on integers that make sense.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/functions-closures/closure-expected-type/expect-infer-supply-two-infers.rs2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/test/run-pass/functions-closures/closure-expected-type/expect-infer-supply-two-infers.rs b/src/test/run-pass/functions-closures/closure-expected-type/expect-infer-supply-two-infers.rs
index fb0bc3c436d..6d5a9876c37 100644
--- a/src/test/run-pass/functions-closures/closure-expected-type/expect-infer-supply-two-infers.rs
+++ b/src/test/run-pass/functions-closures/closure-expected-type/expect-infer-supply-two-infers.rs
@@ -12,7 +12,7 @@ fn expect_free_supply_free<'x>(x: &'x u32) {
         x.push(22_u32);
 
         // ...since we now know the type of `y` and can resolve the method call.
-        y.wrapping_add(1);
+        let _ = y.wrapping_add(1);
     });
 }