-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #62262 - varkor:must_use-adt-components-ii, r=<try>
Extend `#[must_use]` to nested structures Extends the `#[must_use]` lint to apply when `#[must_use]` types are nested within `struct`s (or one-variant `enum`s), making the lint much more generally useful. This is in line with #61100 extending the lint to tuples. Fixes #39524. cc @rust-lang/lang and @rust-lang/compiler for discussion in case this is a controversial change. In particular, we might want to consider allowing annotations on fields containing `#[must_use]` types in user-defined types (e.g. `#[allow(unused_must_use)]`) to opt out of this behaviour, if there are cases where we this this is likely to have frequent false positives. (This is based on top of #62235.)
- Loading branch information
Showing
10 changed files
with
247 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub unsafe fn f(xs: Vec<isize> ) { | ||
xs.iter().map(|_x| { unsafe fn q() { panic!(); } }).collect::<Vec<()>>(); | ||
let _ = xs.iter().map(|_x| { unsafe fn q() { panic!(); } }).collect::<Vec<()>>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ mod other { | |
} | ||
|
||
pub fn foo(){ | ||
1+1; | ||
let _ = 1 + 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#![deny(unused_must_use)] | ||
|
||
#[must_use] | ||
struct S; | ||
|
||
#[must_use] | ||
trait A {} | ||
|
||
struct B; | ||
|
||
impl A for B {} | ||
|
||
struct T(S); | ||
|
||
struct U { | ||
x: (), | ||
y: T, | ||
} | ||
|
||
struct V { | ||
a: S, | ||
} | ||
|
||
struct W { | ||
w: [(u8, Box<dyn A>); 2], | ||
x: u32, | ||
y: (B, B), | ||
z: (S, S), | ||
e: [(u8, Box<dyn A>); 2], | ||
f: S, | ||
} | ||
|
||
fn get_v() -> V { | ||
V { a: S } | ||
} | ||
|
||
struct Z([(u8, Box<dyn A>); 2]); | ||
|
||
fn get_wrapped_arr() -> Z { | ||
Z([(0, Box::new(B)), (0, Box::new(B))]) | ||
} | ||
|
||
fn get_tuple_arr() -> ([(u8, Box<dyn A>); 2],) { | ||
([(0, Box::new(B)), (0, Box::new(B))],) | ||
} | ||
|
||
struct R<T> { | ||
r: T | ||
} | ||
|
||
struct List<T>(T, Option<Box<Self>>); | ||
|
||
fn main() { | ||
S; //~ ERROR unused `S` that must be used | ||
T(S); //~ ERROR unused `S` in field `0` that must be used | ||
U { x: (), y: T(S) }; //~ ERROR unused `S` in field `0` that must be used | ||
get_v(); //~ ERROR unused `S` in field `a` that must be used | ||
V { a: S }; //~ ERROR unused `S` in field `a` that must be used | ||
W { | ||
w: [(0, Box::new(B)), (0, Box::new(B))], | ||
//~^ ERROR unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
x: 0, | ||
y: (B, B), | ||
z: (S, S), | ||
//~^ unused `S` in tuple element 0 that must be used | ||
//~^^ unused `S` in tuple element 1 that must be used | ||
e: [(0, Box::new(B)), (0, Box::new(B))], | ||
//~^ unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
f: S, //~ ERROR unused `S` in field `f` that must be used | ||
}; | ||
get_wrapped_arr(); | ||
//~^ ERROR unused array of boxed `A` trait objects in tuple element 1 that must be use | ||
get_tuple_arr(); | ||
//~^ ERROR unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
R { r: S }; //~ ERROR unused `S` in field `r` that must be used | ||
List(S, Some(Box::new(List(S, None)))); //~ ERROR unused `S` in field `0` that must be used | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
error: unused `S` that must be used | ||
--> $DIR/must_use-adt.rs:54:5 | ||
| | ||
LL | S; | ||
| ^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/must_use-adt.rs:1:9 | ||
| | ||
LL | #![deny(unused_must_use)] | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: unused `S` in field `0` that must be used | ||
--> $DIR/must_use-adt.rs:55:7 | ||
| | ||
LL | T(S); | ||
| ^ | ||
|
||
error: unused `S` in field `0` that must be used | ||
--> $DIR/must_use-adt.rs:56:21 | ||
| | ||
LL | U { x: (), y: T(S) }; | ||
| ^ | ||
|
||
error: unused `S` in field `a` that must be used | ||
--> $DIR/must_use-adt.rs:57:5 | ||
| | ||
LL | get_v(); | ||
| ^^^^^^^^ | ||
|
||
error: unused `S` in field `a` that must be used | ||
--> $DIR/must_use-adt.rs:58:12 | ||
| | ||
LL | V { a: S }; | ||
| ^ | ||
|
||
error: unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
--> $DIR/must_use-adt.rs:60:12 | ||
| | ||
LL | w: [(0, Box::new(B)), (0, Box::new(B))], | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: unused `S` in tuple element 0 that must be used | ||
--> $DIR/must_use-adt.rs:64:13 | ||
| | ||
LL | z: (S, S), | ||
| ^ | ||
|
||
error: unused `S` in tuple element 1 that must be used | ||
--> $DIR/must_use-adt.rs:64:16 | ||
| | ||
LL | z: (S, S), | ||
| ^ | ||
|
||
error: unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
--> $DIR/must_use-adt.rs:67:12 | ||
| | ||
LL | e: [(0, Box::new(B)), (0, Box::new(B))], | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: unused `S` in field `f` that must be used | ||
--> $DIR/must_use-adt.rs:69:12 | ||
| | ||
LL | f: S, | ||
| ^ | ||
|
||
error: unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
--> $DIR/must_use-adt.rs:71:5 | ||
| | ||
LL | get_wrapped_arr(); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: unused array of boxed `A` trait objects in tuple element 1 that must be used | ||
--> $DIR/must_use-adt.rs:73:5 | ||
| | ||
LL | get_tuple_arr(); | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: unused `S` in field `r` that must be used | ||
--> $DIR/must_use-adt.rs:75:12 | ||
| | ||
LL | R { r: S }; | ||
| ^ | ||
|
||
error: unused `S` in field `0` that must be used | ||
--> $DIR/must_use-adt.rs:76:10 | ||
| | ||
LL | List(S, Some(Box::new(List(S, None)))); | ||
| ^ | ||
|
||
error: aborting due to 14 previous errors | ||
|