about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-09-19 13:06:31 +0000
committerbors <bors@rust-lang.org>2017-09-19 13:06:31 +0000
commit325ba23d5525ecdd555f19c7f527bce913dfd756 (patch)
tree6224ff24779d39f49f4e98f3833a61bae159b2df
parent5d5dcae7f15850e7668a38e5da87cf6010e87eba (diff)
parentd09db63d2ff0223c8c0cc2626547f9abe1b06a84 (diff)
downloadrust-325ba23d5525ecdd555f19c7f527bce913dfd756.tar.gz
rust-325ba23d5525ecdd555f19c7f527bce913dfd756.zip
Auto merge of #44620 - zackmdavis:rfc_1940_housekeeping, r=nikomatsakis
RFC 1940 housekeeping

* move test to own directory, as requested in https://github.com/rust-lang/rust/issues/43302#issuecomment-329579185
* exercise trait methods in test
* unstable book section

r? @nikomatsakis
-rw-r--r--src/doc/unstable-book/src/language-features/fn-must-use.md30
-rw-r--r--src/test/ui/lint/fn_must_use.rs35
-rw-r--r--src/test/ui/lint/fn_must_use.stderr18
-rw-r--r--src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.rs70
-rw-r--r--src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.stderr30
5 files changed, 130 insertions, 53 deletions
diff --git a/src/doc/unstable-book/src/language-features/fn-must-use.md b/src/doc/unstable-book/src/language-features/fn-must-use.md
new file mode 100644
index 00000000000..71b6cd663a0
--- /dev/null
+++ b/src/doc/unstable-book/src/language-features/fn-must-use.md
@@ -0,0 +1,30 @@
+# `fn_must_use`
+
+The tracking issue for this feature is [#43302].
+
+[#43302]: https://github.com/rust-lang/rust/issues/43302
+
+------------------------
+
+The `fn_must_use` feature allows functions and methods to be annotated with
+`#[must_use]`, indicating that the `unused_must_use` lint should require their
+return values to be used (similarly to how types annotated with `must_use`,
+most notably `Result`, are linted if not used).
+
+## Examples
+
+```rust
+#![feature(fn_must_use)]
+
+#[must_use]
+fn double(x: i32) -> i32 {
+    2 * x
+}
+
+fn main() {
+    double(4); // warning: unused return value of `double` which must be used
+
+    let _ = double(4); // (no warning)
+}
+
+```
diff --git a/src/test/ui/lint/fn_must_use.rs b/src/test/ui/lint/fn_must_use.rs
deleted file mode 100644
index c549ded4db2..00000000000
--- a/src/test/ui/lint/fn_must_use.rs
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#![feature(fn_must_use)]
-#![warn(unused_must_use)]
-
-struct MyStruct {
-    n: usize
-}
-
-impl MyStruct {
-    #[must_use]
-    fn need_to_use_this_method_value(&self) -> usize {
-        self.n
-    }
-}
-
-#[must_use="it's important"]
-fn need_to_use_this_value() -> bool {
-    false
-}
-
-fn main() {
-    need_to_use_this_value();
-
-    let m = MyStruct { n: 2 };
-    m.need_to_use_this_method_value();
-}
diff --git a/src/test/ui/lint/fn_must_use.stderr b/src/test/ui/lint/fn_must_use.stderr
deleted file mode 100644
index 242837793a0..00000000000
--- a/src/test/ui/lint/fn_must_use.stderr
+++ /dev/null
@@ -1,18 +0,0 @@
-warning: unused return value of `need_to_use_this_value` which must be used: it's important
-  --> $DIR/fn_must_use.rs:31:5
-   |
-31 |     need_to_use_this_value();
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-note: lint level defined here
-  --> $DIR/fn_must_use.rs:12:9
-   |
-12 | #![warn(unused_must_use)]
-   |         ^^^^^^^^^^^^^^^
-
-warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used
-  --> $DIR/fn_must_use.rs:34:5
-   |
-34 |     m.need_to_use_this_method_value();
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
diff --git a/src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.rs b/src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.rs
new file mode 100644
index 00000000000..7eb4c32972a
--- /dev/null
+++ b/src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.rs
@@ -0,0 +1,70 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(fn_must_use)]
+#![warn(unused_must_use)]
+
+struct MyStruct {
+    n: usize,
+}
+
+impl MyStruct {
+    #[must_use]
+    fn need_to_use_this_method_value(&self) -> usize {
+        self.n
+    }
+}
+
+trait EvenNature {
+    #[must_use = "no side effects"]
+    fn is_even(&self) -> bool;
+}
+
+impl EvenNature for MyStruct {
+    fn is_even(&self) -> bool {
+        self.n % 2 == 0
+    }
+}
+
+trait Replaceable {
+    fn replace(&mut self, substitute: usize) -> usize;
+}
+
+impl Replaceable for MyStruct {
+    // ↓ N.b.: `#[must_use]` attribute on a particular trait implementation
+    // method won't work; the attribute should be on the method signature in
+    // the trait's definition.
+    #[must_use]
+    fn replace(&mut self, substitute: usize) -> usize {
+        let previously = self.n;
+        self.n = substitute;
+        previously
+    }
+}
+
+#[must_use = "it's important"]
+fn need_to_use_this_value() -> bool {
+    false
+}
+
+fn main() {
+    need_to_use_this_value();
+
+    let mut m = MyStruct { n: 2 };
+    m.need_to_use_this_method_value();
+    m.is_even(); // trait method!
+
+    m.replace(3);
+
+    2.eq(&3);
+
+    // FIXME: operators should probably be `must_use` if underlying method is
+    2 == 3;
+}
diff --git a/src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.stderr b/src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.stderr
new file mode 100644
index 00000000000..69755c89b48
--- /dev/null
+++ b/src/test/ui/rfc_1940-must_use_on_functions/fn_must_use.stderr
@@ -0,0 +1,30 @@
+warning: unused return value of `need_to_use_this_value` which must be used: it's important
+  --> $DIR/fn_must_use.rs:58:5
+   |
+58 |     need_to_use_this_value();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/fn_must_use.rs:12:9
+   |
+12 | #![warn(unused_must_use)]
+   |         ^^^^^^^^^^^^^^^
+
+warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used
+  --> $DIR/fn_must_use.rs:61:5
+   |
+61 |     m.need_to_use_this_method_value();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+warning: unused return value of `EvenNature::is_even` which must be used: no side effects
+  --> $DIR/fn_must_use.rs:62:5
+   |
+62 |     m.is_even(); // trait method!
+   |     ^^^^^^^^^^^^
+
+warning: unused return value of `std::cmp::PartialEq::eq` which must be used
+  --> $DIR/fn_must_use.rs:66:5
+   |
+66 |     2.eq(&3);
+   |     ^^^^^^^^^
+