From 01f9664018d798bce6cd5520b05ecedf45541c22 Mon Sep 17 00:00:00 2001 From: chansuke Date: Sat, 25 Jul 2020 00:46:04 +0900 Subject: [PATCH] Fix unwrap not to lint the #[test]ing code --- clippy_lints/src/unwrap.rs | 11 ++++++++++- tests/ui/unwrap.rs | 16 ++++++++++++++++ tests/ui/unwrap.stderr | 22 +++++++++------------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/unwrap.rs b/clippy_lints/src/unwrap.rs index f2bbde28c2ab..7008589bcab3 100644 --- a/clippy_lints/src/unwrap.rs +++ b/clippy_lints/src/unwrap.rs @@ -4,7 +4,7 @@ use crate::utils::{ }; use if_chain::if_chain; use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor}; -use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Path, QPath, UnOp}; +use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Path, QPath, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::map::Map; use rustc_middle::lint::in_external_macro; @@ -210,6 +210,11 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> { declare_lint_pass!(Unwrap => [PANICKING_UNWRAP, UNNECESSARY_UNWRAP]); impl<'tcx> LateLintPass<'tcx> for Unwrap { + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { + if is_test_module_or_function(item) { + return; + } + } fn check_fn( &mut self, cx: &LateContext<'tcx>, @@ -231,3 +236,7 @@ impl<'tcx> LateLintPass<'tcx> for Unwrap { walk_fn(&mut v, kind, decl, body.id(), span, fn_id); } } + +fn is_test_module_or_function(item: &Item<'_>) -> bool { + matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test") +} diff --git a/tests/ui/unwrap.rs b/tests/ui/unwrap.rs index a4a3cd1d3797..beccccd08aea 100644 --- a/tests/ui/unwrap.rs +++ b/tests/ui/unwrap.rs @@ -10,7 +10,23 @@ fn unwrap_result() { let _ = res.unwrap(); } +// Should not get linted. +#[test] +fn with_test() { + let opt = Some(0); + let _ = opt.unwrap(); +} + +// Should not get linted. +#[cfg(test)] +fn with_cfg_test() { + let res: Result = Ok(0); + let _ = res.unwrap(); +} + fn main() { unwrap_option(); unwrap_result(); + with_test(); + with_cfg_test(); } diff --git a/tests/ui/unwrap.stderr b/tests/ui/unwrap.stderr index 4f0858005f6e..bd05819f8dfb 100644 --- a/tests/ui/unwrap.stderr +++ b/tests/ui/unwrap.stderr @@ -1,19 +1,15 @@ -error: used `unwrap()` on `an Option` value - --> $DIR/unwrap.rs:5:13 +error[E0425]: cannot find function `with_test` in this scope + --> $DIR/unwrap.rs:30:5 | -LL | let _ = opt.unwrap(); - | ^^^^^^^^^^^^ - | - = note: `-D clippy::unwrap-used` implied by `-D warnings` - = help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message +LL | with_test(); + | ^^^^^^^^^ not found in this scope -error: used `unwrap()` on `a Result` value - --> $DIR/unwrap.rs:10:13 - | -LL | let _ = res.unwrap(); - | ^^^^^^^^^^^^ +error[E0425]: cannot find function `with_cfg_test` in this scope + --> $DIR/unwrap.rs:31:5 | - = help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message +LL | with_cfg_test(); + | ^^^^^^^^^^^^^ not found in this scope error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0425`.