fix: fix tests

This commit is contained in:
Max Baumann 2022-03-17 19:13:44 +01:00
parent f49a2c3457
commit 3f00f074de
No known key found for this signature in database
GPG Key ID: 20FA1609B03B1D6D
3 changed files with 16 additions and 18 deletions

View File

@ -1,11 +1,10 @@
use if_chain::if_chain;
use rustc_hir::*;
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::diagnostics::span_lint_and_help;
use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::ty::is_type_diagnostic_item;
use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, Span};
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -39,7 +38,6 @@ declare_lint_pass!(UseUnwrapOr => [USE_UNWRAP_OR]);
impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr { impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// look for x.or().unwrap() // look for x.or().unwrap()
if_chain! { if_chain! {
if let ExprKind::MethodCall(path, args, unwrap_span) = expr.kind; if let ExprKind::MethodCall(path, args, unwrap_span) = expr.kind;
@ -52,13 +50,13 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
let title; let title;
let arg = &caller_args[1]; // the argument or(xyz) is called with 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"; title = ".or(Some(…)).unwrap() found";
if !is(arg, "Some") { if !is(arg, "Some") {
return; 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"; title = ".or(Ok(…)).unwrap() found";
if !is(arg, "Ok") { if !is(arg, "Ok") {
return; return;
@ -90,16 +88,14 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
fn is<'a>(expr: &Expr<'a>, name: &str) -> bool { fn is<'a>(expr: &Expr<'a>, name: &str) -> bool {
if_chain! { if_chain! {
if let ExprKind::Call(some_expr, _some_args) = expr.kind; if let ExprKind::Call(some_expr, _some_args) = expr.kind;
if let ExprKind::Path(path) = &some_expr.kind; if let ExprKind::Path(QPath::Resolved(_, path)) = &some_expr.kind;
if let QPath::Resolved(_, path) = path;
if let Some(path_segment) = path.segments.first(); if let Some(path_segment) = path.segments.first();
if path_segment.ident.name.as_str() == name; if path_segment.ident.name.as_str() == name;
then { then {
return true; true
} }
else { else {
return false; false
} }
} }
} }

View File

@ -2,8 +2,10 @@
struct SomeStruct {} struct SomeStruct {}
impl SomeStruct { impl SomeStruct {
fn or(self, _: Option<Self>) -> Self { self } fn or(self, _: Option<Self>) -> Self {
fn unwrap(&self){} self
}
fn unwrap(&self) {}
} }
fn main() { fn main() {

View File

@ -1,5 +1,5 @@
error: .or(Some(…)).unwrap() found 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 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 = help: use `unwrap_or()` instead
error: .or(Ok(…)).unwrap() found 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 LL | let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^