From 3f00f074de3f6debe594e11066864a0da1dfe4af Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Thu, 17 Mar 2022 19:13:44 +0100 Subject: fix: fix tests --- clippy_lints/src/use_unwrap_or.rs | 22 +++++++++------------- tests/ui/use_unwrap_or.rs | 6 ++++-- tests/ui/use_unwrap_or.stderr | 4 ++-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/clippy_lints/src/use_unwrap_or.rs b/clippy_lints/src/use_unwrap_or.rs index f42e28246d4..05369000444 100644 --- a/clippy_lints/src/use_unwrap_or.rs +++ b/clippy_lints/src/use_unwrap_or.rs @@ -1,11 +1,10 @@ +use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::ty::is_type_diagnostic_item; use if_chain::if_chain; -use rustc_hir::*; +use rustc_hir::{Expr, ExprKind, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::{Span, sym}; -use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::ty::is_type_diagnostic_item; - +use rustc_span::{sym, Span}; declare_clippy_lint! { /// ### What it does @@ -39,7 +38,6 @@ declare_lint_pass!(UseUnwrapOr => [USE_UNWRAP_OR]); 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, args, unwrap_span) = expr.kind; @@ -52,13 +50,13 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr { let title; let arg = &caller_args[1]; // the argument or(xyz) is called with - if is_type_diagnostic_item(&cx, ty, sym::Option) { + if is_type_diagnostic_item(cx, ty, sym::Option) { title = ".or(Some(…)).unwrap() found"; if !is(arg, "Some") { return; } - } else if is_type_diagnostic_item(&cx, ty, sym::Result) { + } else if is_type_diagnostic_item(cx, ty, sym::Result) { title = ".or(Ok(…)).unwrap() found"; if !is(arg, "Ok") { return; @@ -90,16 +88,14 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr { fn is<'a>(expr: &Expr<'a>, name: &str) -> bool { if_chain! { if let ExprKind::Call(some_expr, _some_args) = expr.kind; - if let ExprKind::Path(path) = &some_expr.kind; - if let QPath::Resolved(_, path) = path; + if let ExprKind::Path(QPath::Resolved(_, path)) = &some_expr.kind; if let Some(path_segment) = path.segments.first(); if path_segment.ident.name.as_str() == name; then { - return true; + true } else { - return false; + false } } } - diff --git a/tests/ui/use_unwrap_or.rs b/tests/ui/use_unwrap_or.rs index 0bfabaae885..685fab80206 100644 --- a/tests/ui/use_unwrap_or.rs +++ b/tests/ui/use_unwrap_or.rs @@ -2,8 +2,10 @@ struct SomeStruct {} impl SomeStruct { - fn or(self, _: Option) -> Self { self } - fn unwrap(&self){} + fn or(self, _: Option) -> Self { + self + } + fn unwrap(&self) {} } fn main() { diff --git a/tests/ui/use_unwrap_or.stderr b/tests/ui/use_unwrap_or.stderr index 5bbba41f7ac..2e1e920795d 100644 --- a/tests/ui/use_unwrap_or.stderr +++ b/tests/ui/use_unwrap_or.stderr @@ -1,5 +1,5 @@ error: .or(Some(…)).unwrap() found - --> $DIR/use_unwrap_or.rs:11:20 + --> $DIR/use_unwrap_or.rs:13: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:14:20 + --> $DIR/use_unwrap_or.rs:16:20 | LL | let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- cgit 1.4.1-3-g733a5