Skip to content
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
1 change: 1 addition & 0 deletions clippy_lints/src/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
// Shouldn't lint when `expr` is in macro.
if expr.span.in_external_macro(self.cx.tcx.sess.source_map()) {
walk_expr(self, expr);
return;
}
// Skip checking inside closures since they are visited through `Unwrap::check_fn()` already.
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/checked_unwrap/simple_conditionals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,28 @@ const ISSUE14763: fn(Option<String>) = |x| {
}
};

fn issue12295() {
let option = Some(());

if option.is_some() {
println!("{:?}", option.unwrap());
//~^ unnecessary_unwrap
} else {
println!("{:?}", option.unwrap());
//~^ panicking_unwrap
}

let result = Ok::<(), ()>(());

if result.is_ok() {
println!("{:?}", result.unwrap());
//~^ unnecessary_unwrap
} else {
println!("{:?}", result.unwrap());
//~^ panicking_unwrap
}
}

fn check_expect() {
let x = Some(());
if x.is_some() {
Expand Down
36 changes: 35 additions & 1 deletion tests/ui/checked_unwrap/simple_conditionals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,40 @@ LL | if x.is_some() {
LL | _ = x.unwrap();
| ^^^^^^^^^^

error: called `unwrap` on `option` after checking its variant with `is_some`
--> tests/ui/checked_unwrap/simple_conditionals.rs:280:26
|
LL | if option.is_some() {
| ------------------- help: try: `if let Some(<item>) = option`
LL | println!("{:?}", option.unwrap());
| ^^^^^^^^^^^^^^^

error: this call to `unwrap()` will always panic
--> tests/ui/checked_unwrap/simple_conditionals.rs:283:26
|
LL | if option.is_some() {
| ---------------- because of this check
...
LL | println!("{:?}", option.unwrap());
| ^^^^^^^^^^^^^^^

error: called `unwrap` on `result` after checking its variant with `is_ok`
--> tests/ui/checked_unwrap/simple_conditionals.rs:290:26
|
LL | if result.is_ok() {
| ----------------- help: try: `if let Ok(<item>) = result`
LL | println!("{:?}", result.unwrap());
| ^^^^^^^^^^^^^^^

error: this call to `unwrap()` will always panic
--> tests/ui/checked_unwrap/simple_conditionals.rs:293:26
|
LL | if result.is_ok() {
| -------------- because of this check
...
LL | println!("{:?}", result.unwrap());
| ^^^^^^^^^^^^^^^

error: creating a shared reference to mutable static
--> tests/ui/checked_unwrap/simple_conditionals.rs:183:12
|
Expand All @@ -332,5 +366,5 @@ LL | if X.is_some() {
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
= note: `#[deny(static_mut_refs)]` (part of `#[deny(rust_2024_compatibility)]`) on by default

error: aborting due to 36 previous errors
error: aborting due to 40 previous errors

Loading