about summary refs log tree commit diff
path: root/src/test/ui/thinlto
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/ui/thinlto
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/ui/thinlto')
0 files changed, 0 insertions, 0 deletions