-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unnecessary_semicolon
: do not lint if it may cause borrow errors (#…
…14049) Before edition 2024, some temporaries used in scrutinees of a `match` used as the last expression of a block may outlive some referenced local variables. Prevent those cases from happening by checking that alive temporaries with significant drop do have a static lifetime. The check is performed only for edition 2021 and earlier, and for the last statement if it would become the last expression of the block. changelog: [`unnecessary_semicolon`]: prevent borrow errors in editions lower than 2024 r? @y21
- Loading branch information
Showing
9 changed files
with
269 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//@revisions: edition2021 edition2024 | ||
//@[edition2021] edition:2021 | ||
//@[edition2024] edition:2024 | ||
|
||
#![warn(clippy::unnecessary_semicolon)] | ||
#![feature(postfix_match)] | ||
#![allow(clippy::single_match)] | ||
|
||
fn no_lint(mut x: u32) -> Option<u32> { | ||
Some(())?; | ||
|
||
{ | ||
let y = 3; | ||
dbg!(x + y) | ||
}; | ||
|
||
{ | ||
let (mut a, mut b) = (10, 20); | ||
(a, b) = (b + 1, a + 1); | ||
} | ||
|
||
Some(0) | ||
} | ||
|
||
fn main() { | ||
let mut a = 3; | ||
if a == 2 { | ||
println!("This is weird"); | ||
} | ||
//~^ ERROR: unnecessary semicolon | ||
|
||
a.match { | ||
3 => println!("three"), | ||
_ => println!("not three"), | ||
} | ||
//~^ ERROR: unnecessary semicolon | ||
} | ||
|
||
// This is a problem in edition 2021 and below | ||
fn borrow_issue() { | ||
let v = std::cell::RefCell::new(Some(vec![1])); | ||
match &*v.borrow() { | ||
Some(v) => { | ||
dbg!(v); | ||
}, | ||
None => {}, | ||
}; | ||
} | ||
|
||
fn no_borrow_issue(a: u32, b: u32) { | ||
match Some(a + b) { | ||
Some(v) => { | ||
dbg!(v); | ||
}, | ||
None => {}, | ||
} | ||
} |
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,23 @@ | ||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:29:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
| | ||
= note: `-D clippy::unnecessary-semicolon` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_semicolon)]` | ||
|
||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:35:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
|
||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:56:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
|
||
error: aborting due to 3 previous errors | ||
|
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,57 @@ | ||
//@revisions: edition2021 edition2024 | ||
//@[edition2021] edition:2021 | ||
//@[edition2024] edition:2024 | ||
|
||
#![warn(clippy::unnecessary_semicolon)] | ||
#![feature(postfix_match)] | ||
#![allow(clippy::single_match)] | ||
|
||
fn no_lint(mut x: u32) -> Option<u32> { | ||
Some(())?; | ||
|
||
{ | ||
let y = 3; | ||
dbg!(x + y) | ||
}; | ||
|
||
{ | ||
let (mut a, mut b) = (10, 20); | ||
(a, b) = (b + 1, a + 1); | ||
} | ||
|
||
Some(0) | ||
} | ||
|
||
fn main() { | ||
let mut a = 3; | ||
if a == 2 { | ||
println!("This is weird"); | ||
} | ||
//~^ ERROR: unnecessary semicolon | ||
|
||
a.match { | ||
3 => println!("three"), | ||
_ => println!("not three"), | ||
} | ||
//~^ ERROR: unnecessary semicolon | ||
} | ||
|
||
// This is a problem in edition 2021 and below | ||
fn borrow_issue() { | ||
let v = std::cell::RefCell::new(Some(vec![1])); | ||
match &*v.borrow() { | ||
Some(v) => { | ||
dbg!(v); | ||
}, | ||
None => {}, | ||
} | ||
} | ||
|
||
fn no_borrow_issue(a: u32, b: u32) { | ||
match Some(a + b) { | ||
Some(v) => { | ||
dbg!(v); | ||
}, | ||
None => {}, | ||
} | ||
} |
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,29 @@ | ||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:29:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
| | ||
= note: `-D clippy::unnecessary-semicolon` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_semicolon)]` | ||
|
||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:35:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
|
||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:47:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
|
||
error: unnecessary semicolon | ||
--> tests/ui/unnecessary_semicolon.rs:56:6 | ||
| | ||
LL | }; | ||
| ^ help: remove | ||
|
||
error: aborting due to 4 previous errors | ||
|
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