Skip to content

Commit b1f89ee

Browse files
committed
or_fun_call: trigger on unsafe blocks
1 parent 56161b2 commit b1f89ee

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
9+
use rustc_hir::{BlockCheckMode, UnsafeSource};
910
use rustc_lint::LateContext;
1011
use rustc_middle::ty;
1112
use rustc_span::source_map::Span;
@@ -154,7 +155,6 @@ pub(super) fn check<'tcx>(
154155
}
155156
}
156157
}
157-
158158
if args.len() == 2 {
159159
match args[1].kind {
160160
hir::ExprKind::Call(ref fun, ref or_args) => {
@@ -167,7 +167,16 @@ pub(super) fn check<'tcx>(
167167
hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => {
168168
check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, None);
169169
},
170-
_ => {},
170+
hir::ExprKind::Block(block, _) => {
171+
if let BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) = block.rules {
172+
if let Some(block_expr) = block.expr {
173+
if let hir::ExprKind::MethodCall(..) = block_expr.kind {
174+
check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, None);
175+
}
176+
}
177+
}
178+
},
179+
_ => (),
171180
}
172181
}
173182
}

tests/ui/or_fun_call.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,18 @@ fn f() -> Option<()> {
132132
Some(())
133133
}
134134

135+
mod issue6675 {
136+
unsafe fn foo() {
137+
let mut s = "test".to_owned();
138+
None.unwrap_or_else(|| s.as_mut_vec());
139+
}
140+
141+
fn bar() {
142+
let mut s = "test".to_owned();
143+
None.unwrap_or_else(|| unsafe { s.as_mut_vec() });
144+
#[rustfmt::skip]
145+
None.unwrap_or_else(|| unsafe { s.as_mut_vec() });
146+
}
147+
}
148+
135149
fn main() {}

tests/ui/or_fun_call.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,18 @@ fn f() -> Option<()> {
132132
Some(())
133133
}
134134

135+
mod issue6675 {
136+
unsafe fn foo() {
137+
let mut s = "test".to_owned();
138+
None.unwrap_or(s.as_mut_vec());
139+
}
140+
141+
fn bar() {
142+
let mut s = "test".to_owned();
143+
None.unwrap_or(unsafe { s.as_mut_vec() });
144+
#[rustfmt::skip]
145+
None.unwrap_or( unsafe { s.as_mut_vec() } );
146+
}
147+
}
148+
135149
fn main() {}

tests/ui/or_fun_call.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,23 @@ error: use of `or` followed by a function call
114114
LL | .or(Some(Bar(b, Duration::from_secs(2))));
115115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some(Bar(b, Duration::from_secs(2))))`
116116

117-
error: aborting due to 19 previous errors
117+
error: use of `unwrap_or` followed by a function call
118+
--> $DIR/or_fun_call.rs:138:14
119+
|
120+
LL | None.unwrap_or(s.as_mut_vec());
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| s.as_mut_vec())`
122+
123+
error: use of `unwrap_or` followed by a function call
124+
--> $DIR/or_fun_call.rs:143:14
125+
|
126+
LL | None.unwrap_or(unsafe { s.as_mut_vec() });
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| unsafe { s.as_mut_vec() })`
128+
129+
error: use of `unwrap_or` followed by a function call
130+
--> $DIR/or_fun_call.rs:145:14
131+
|
132+
LL | None.unwrap_or( unsafe { s.as_mut_vec() } );
133+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| unsafe { s.as_mut_vec() })`
134+
135+
error: aborting due to 22 previous errors
118136

0 commit comments

Comments
 (0)