Skip to content

Commit

Permalink
Fix unwrap not to lint the #[test]ing code
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Aug 6, 2020
1 parent 2d4c337 commit 01f9664
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
11 changes: 10 additions & 1 deletion clippy_lints/src/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand All @@ -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")
}
16 changes: 16 additions & 0 deletions tests/ui/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8, ()> = Ok(0);
let _ = res.unwrap();
}

fn main() {
unwrap_option();
unwrap_result();
with_test();
with_cfg_test();
}
22 changes: 9 additions & 13 deletions tests/ui/unwrap.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 01f9664

Please sign in to comment.