or_fun_call: trigger on unsafe blocks

This commit is contained in:
Mateusz Gacek 2021-03-17 20:29:31 +01:00
parent 56161b2982
commit b1f89ee02f
4 changed files with 58 additions and 3 deletions

View File

@ -6,6 +6,7 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type};
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