Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for unit_bindings lint #131999

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/ui/lint/unit_bindings.deny_level.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error: binding has unit type `()`
--> $DIR/unit_bindings.rs:50:5
|
LL | let _ = expr;
| ^^^^-^^^^^^^^
| |
| this pattern is inferred to be the unit type `()`
|
note: the lint level is defined here
--> $DIR/unit_bindings.rs:22:30
|
LL | #![cfg_attr(deny_level, deny(unit_bindings))]
| ^^^^^^^^^^^^^

error: binding has unit type `()`
--> $DIR/unit_bindings.rs:51:5
|
LL | let pat = expr;
| ^^^^---^^^^^^^^
| |
| this pattern is inferred to be the unit type `()`

error: binding has unit type `()`
--> $DIR/unit_bindings.rs:52:5
|
LL | let _pat = expr;
| ^^^^----^^^^^^^^
| |
| this pattern is inferred to be the unit type `()`

error: binding has unit type `()`
--> $DIR/unit_bindings.rs:55:5
|
LL | let list = v.sort();
| ^^^^----^^^^^^^^^^^^
| |
| this pattern is inferred to be the unit type `()`

error: aborting due to 4 previous errors

60 changes: 60 additions & 0 deletions tests/ui/lint/unit_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Basic checks for `unit_bindings` lint.
//!
//! The `unit_bindings` lint tries to detect cases like `let list = list.sort()`. The lint will
//! trigger on bindings that have the unit `()` type **except** if:
//!
//! - The user wrote `()` on either side, i.e.
//! - `let () = <expr>;` or `let <expr> = ();`
//! - `let _ = ();`
//! - The binding occurs within macro expansions, e.g. `foo!();`.
//! - The user explicitly provided type annotations, e.g. `let x: () = <expr>`.
//!
//! Examples where the lint *should* fire on include:
//!
//! - `let _ = <expr>;`
//! - `let pat = <expr>;`
//! - `let _pat = <expr>;`

//@ revisions: default_level deny_level
//@[default_level] check-pass (`unit_bindings` is currently allow-by-default)

#![allow(unused)]
#![cfg_attr(deny_level, deny(unit_bindings))]

// The `list` binding below should trigger the lint if it's not contained in a macro expansion.
macro_rules! expands_to_sus {
() => {
let mut v = [1, 2, 3];
let list = v.sort();
}
}

// No warning for `y` and `z` because it is provided as type parameter.
fn ty_param_check<T: Copy>(x: T) {
let y = x;
let z: T = x;
}

fn main() {
// No warning if user explicitly wrote `()` on either side.
let expr = ();
let () = expr;
let _ = ();
// No warning if user explicitly annotates the unit type on the binding.
let pat: () = expr;
// No warning for let bindings with unit type in macro expansions.
expands_to_sus!();
// No warning for unit bindings in generic fns.
ty_param_check(());

let _ = expr; //[deny_level]~ ERROR binding has unit type
let pat = expr; //[deny_level]~ ERROR binding has unit type
let _pat = expr; //[deny_level]~ ERROR binding has unit type

let mut v = [1, 2, 3];
let list = v.sort(); //[deny_level]~ ERROR binding has unit type

// Limitation: the lint currently does not fire on nested unit LHS bindings, i.e.
// this will not currently trigger the lint.
let (nested, _) = (expr, 0i32);
}
Loading