|
| 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