mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-06 13:44:13 +00:00
feat: add tests and fix existing ones
This commit is contained in:
parent
17ff85081d
commit
44c62c9aa2
@ -47,12 +47,12 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
// look for x.or().unwrap()
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path, [unwrap_self], unwrap_span) = expr.kind;
|
||||
if let ExprKind::MethodCall(path, [unwrap_self], unwrap_span) = &expr.kind;
|
||||
if path.ident.name == sym::unwrap;
|
||||
if let ExprKind::MethodCall(caller_path, [or_self, or_arg], or_span) = unwrap_self.kind;
|
||||
if let ExprKind::MethodCall(caller_path, [or_self, or_arg], or_span) = &unwrap_self.kind;
|
||||
if caller_path.ident.name == sym::or;
|
||||
then {
|
||||
let ty = cx.typeck_results().expr_ty(&or_self); // get type of x (we later check if it's Option or Result)
|
||||
let ty = cx.typeck_results().expr_ty(or_self); // get type of x (we later check if it's Option or Result)
|
||||
let title;
|
||||
|
||||
if is_type_diagnostic_item(cx, ty, sym::Option) {
|
||||
@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
USE_UNWRAP_OR,
|
||||
or_span.to(unwrap_span),
|
||||
or_span.to(*unwrap_span),
|
||||
title,
|
||||
None,
|
||||
"use `unwrap_or()` instead"
|
||||
|
@ -1,4 +1,5 @@
|
||||
#![warn(clippy::use_unwrap_or)]
|
||||
#![allow(clippy::map_identity)]
|
||||
|
||||
struct SomeStruct {}
|
||||
impl SomeStruct {
|
||||
@ -8,6 +9,14 @@ impl SomeStruct {
|
||||
fn unwrap(&self) {}
|
||||
}
|
||||
|
||||
struct SomeOtherStruct {}
|
||||
impl SomeOtherStruct {
|
||||
fn or(self) -> Self {
|
||||
self
|
||||
}
|
||||
fn unwrap(&self) {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let option: Option<&str> = None;
|
||||
let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
|
||||
@ -19,6 +28,9 @@ fn main() {
|
||||
let instance = SomeStruct {};
|
||||
let _ = instance.or(Some(SomeStruct {})).unwrap(); // should not trigger lint
|
||||
|
||||
let instance = SomeOtherStruct {};
|
||||
let _ = instance.or().unwrap(); // should not trigger lint and should not panic
|
||||
|
||||
// None in or
|
||||
let option: Option<&str> = None;
|
||||
let _ = option.or(None).unwrap(); // should not trigger lint
|
||||
@ -26,4 +38,8 @@ fn main() {
|
||||
// Not Err in or
|
||||
let result: Result<&str, &str> = Err("Error");
|
||||
let _ = result.or::<&str>(Err("Other Error")).unwrap(); // should not trigger lint
|
||||
|
||||
// other function between
|
||||
let option: Option<&str> = None;
|
||||
let _ = option.or(Some("fallback")).map(|v| v).unwrap(); // should not trigger lint
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: .or(Some(…)).unwrap() found
|
||||
--> $DIR/use_unwrap_or.rs:13:20
|
||||
--> $DIR/use_unwrap_or.rs:22:20
|
||||
|
|
||||
LL | let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -8,7 +8,7 @@ LL | let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
|
||||
= help: use `unwrap_or()` instead
|
||||
|
||||
error: .or(Ok(…)).unwrap() found
|
||||
--> $DIR/use_unwrap_or.rs:16:20
|
||||
--> $DIR/use_unwrap_or.rs:25:20
|
||||
|
|
||||
LL | let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
Loading…
Reference in New Issue
Block a user