Auto merge of #6928 - mgacek8:issue6675_or_fun_call_unsafe_blocks, r=phansch

or_fun_call: trigger on unsafe blocks

fixes #6675
changelog: or_fun_call: trigger on unsafe blocks
This commit is contained in:
bors 2021-03-18 15:55:06 +00:00
commit 36aee1c526
4 changed files with 58 additions and 3 deletions

View file

@ -6,6 +6,7 @@ use clippy_utils::{contains_return, get_trait_def_id, last_path_segment, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::{BlockCheckMode, UnsafeSource};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::source_map::Span;
@ -154,7 +155,6 @@ pub(super) fn check<'tcx>(
}
}
}
if args.len() == 2 {
match args[1].kind {
hir::ExprKind::Call(ref fun, ref or_args) => {
@ -167,7 +167,16 @@ pub(super) fn check<'tcx>(
hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => {
check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, None);
},
_ => {},
hir::ExprKind::Block(block, _) => {
if let BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) = block.rules {
if let Some(block_expr) = block.expr {
if let hir::ExprKind::MethodCall(..) = block_expr.kind {
check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, None);
}
}
}
},
_ => (),
}
}
}

View file

@ -132,4 +132,18 @@ fn f() -> Option<()> {
Some(())
}
mod issue6675 {
unsafe fn foo() {
let mut s = "test".to_owned();
None.unwrap_or_else(|| s.as_mut_vec());
}
fn bar() {
let mut s = "test".to_owned();
None.unwrap_or_else(|| unsafe { s.as_mut_vec() });
#[rustfmt::skip]
None.unwrap_or_else(|| unsafe { s.as_mut_vec() });
}
}
fn main() {}

View file

@ -132,4 +132,18 @@ fn f() -> Option<()> {
Some(())
}
mod issue6675 {
unsafe fn foo() {
let mut s = "test".to_owned();
None.unwrap_or(s.as_mut_vec());
}
fn bar() {
let mut s = "test".to_owned();
None.unwrap_or(unsafe { s.as_mut_vec() });
#[rustfmt::skip]
None.unwrap_or( unsafe { s.as_mut_vec() } );
}
}
fn main() {}

View file

@ -114,5 +114,23 @@ error: use of `or` followed by a function call
LL | .or(Some(Bar(b, Duration::from_secs(2))));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some(Bar(b, Duration::from_secs(2))))`
error: aborting due to 19 previous errors
error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:138:14
|
LL | None.unwrap_or(s.as_mut_vec());
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| s.as_mut_vec())`
error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:143:14
|
LL | None.unwrap_or(unsafe { s.as_mut_vec() });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| unsafe { s.as_mut_vec() })`
error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:145:14
|
LL | None.unwrap_or( unsafe { s.as_mut_vec() } );
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| unsafe { s.as_mut_vec() })`
error: aborting due to 22 previous errors