Skip to content

Commit 70ce711

Browse files
committed
unit_bindings: improve test coverage
1 parent 3e33bda commit 70ce711

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Diff for: tests/ui/lint/unit_bindings.deny_level.stderr

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: binding has unit type `()`
2+
--> $DIR/unit_bindings.rs:50:5
3+
|
4+
LL | let _ = expr;
5+
| ^^^^-^^^^^^^^
6+
| |
7+
| this pattern is inferred to be the unit type `()`
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/unit_bindings.rs:22:30
11+
|
12+
LL | #![cfg_attr(deny_level, deny(unit_bindings))]
13+
| ^^^^^^^^^^^^^
14+
15+
error: binding has unit type `()`
16+
--> $DIR/unit_bindings.rs:51:5
17+
|
18+
LL | let pat = expr;
19+
| ^^^^---^^^^^^^^
20+
| |
21+
| this pattern is inferred to be the unit type `()`
22+
23+
error: binding has unit type `()`
24+
--> $DIR/unit_bindings.rs:52:5
25+
|
26+
LL | let _pat = expr;
27+
| ^^^^----^^^^^^^^
28+
| |
29+
| this pattern is inferred to be the unit type `()`
30+
31+
error: binding has unit type `()`
32+
--> $DIR/unit_bindings.rs:55:5
33+
|
34+
LL | let list = v.sort();
35+
| ^^^^----^^^^^^^^^^^^
36+
| |
37+
| this pattern is inferred to be the unit type `()`
38+
39+
error: aborting due to 4 previous errors
40+

Diff for: tests/ui/lint/unit_bindings.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Basic checks for `unit_bindings` lint.
2+
//!
3+
//! The `unit_bindings` lint tries to detect cases like `let list = list.sort()`. The lint will
4+
//! trigger on bindings that have the unit `()` type **except** if:
5+
//!
6+
//! - The user wrote `()` on either side, i.e.
7+
//! - `let () = <expr>;` or `let <expr> = ();`
8+
//! - `let _ = ();`
9+
//! - The binding occurs within macro expansions, e.g. `foo!();`.
10+
//! - The user explicitly provided type annotations, e.g. `let x: () = <expr>`.
11+
//!
12+
//! Examples where the lint *should* fire on include:
13+
//!
14+
//! - `let _ = <expr>;`
15+
//! - `let pat = <expr>;`
16+
//! - `let _pat = <expr>;`
17+
18+
//@ revisions: default_level deny_level
19+
//@[default_level] check-pass (`unit_bindings` is currently allow-by-default)
20+
21+
#![allow(unused)]
22+
#![cfg_attr(deny_level, deny(unit_bindings))]
23+
24+
// The `list` binding below should trigger the lint if it's not contained in a macro expansion.
25+
macro_rules! expands_to_sus {
26+
() => {
27+
let mut v = [1, 2, 3];
28+
let list = v.sort();
29+
}
30+
}
31+
32+
// No warning for `y` and `z` because it is provided as type parameter.
33+
fn ty_param_check<T: Copy>(x: T) {
34+
let y = x;
35+
let z: T = x;
36+
}
37+
38+
fn main() {
39+
// No warning if user explicitly wrote `()` on either side.
40+
let expr = ();
41+
let () = expr;
42+
let _ = ();
43+
// No warning if user explicitly annotates the unit type on the binding.
44+
let pat: () = expr;
45+
// No warning for let bindings with unit type in macro expansions.
46+
expands_to_sus!();
47+
// No warning for unit bindings in generic fns.
48+
ty_param_check(());
49+
50+
let _ = expr; //[deny_level]~ ERROR binding has unit type
51+
let pat = expr; //[deny_level]~ ERROR binding has unit type
52+
let _pat = expr; //[deny_level]~ ERROR binding has unit type
53+
54+
let mut v = [1, 2, 3];
55+
let list = v.sort(); //[deny_level]~ ERROR binding has unit type
56+
57+
// Limitation: the lint currently does not fire on nested unit LHS bindings, i.e.
58+
// this will not currently trigger the lint.
59+
let (nested, _) = (expr, 0i32);
60+
}

0 commit comments

Comments
 (0)