Skip to content

Commit c8ad132

Browse files
author
chansuke
committed
Move is_test_module_or_function to utils
1 parent ded0fb6 commit c8ad132

File tree

6 files changed

+54
-49
lines changed

6 files changed

+54
-49
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
992992
store.register_late_pass(|| box map_unit_fn::MapUnit);
993993
store.register_late_pass(|| box inherent_impl::MultipleInherentImpl::default());
994994
store.register_late_pass(|| box neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd);
995-
store.register_late_pass(|| box unwrap::Unwrap);
995+
store.register_late_pass(|| box unwrap::Unwrap::new());
996996
store.register_late_pass(|| box duration_subsec::DurationSubsec);
997997
store.register_late_pass(|| box default_trait_access::DefaultTraitAccess);
998998
store.register_late_pass(|| box indexing_slicing::IndexingSlicing);

clippy_lints/src/unwrap.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use crate::utils::{
2-
differing_macro_contexts, higher::if_block, is_type_diagnostic_item, span_lint_and_then,
3-
usage::is_potentially_mutated,
2+
differing_macro_contexts, higher::if_block, is_test_module_or_function, is_type_diagnostic_item,
3+
span_lint_and_then, usage::is_potentially_mutated,
44
};
55
use if_chain::if_chain;
66
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor};
7-
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Path, QPath, UnOp};
7+
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Item, Path, QPath, UnOp};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::hir::map::Map;
1010
use rustc_middle::lint::in_external_macro;
1111
use rustc_middle::ty::Ty;
12-
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1313
use rustc_span::source_map::Span;
1414

1515
declare_clippy_lint! {
@@ -207,12 +207,23 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
207207
}
208208
}
209209

210-
declare_lint_pass!(Unwrap => [PANICKING_UNWRAP, UNNECESSARY_UNWRAP]);
210+
#[derive(Default)]
211+
pub struct Unwrap {
212+
test_modules_deep: u32,
213+
}
214+
215+
impl Unwrap {
216+
pub fn new() -> Self {
217+
Self { test_modules_deep: 0 }
218+
}
219+
}
220+
221+
impl_lint_pass!(Unwrap => [PANICKING_UNWRAP, UNNECESSARY_UNWRAP]);
211222

212223
impl<'tcx> LateLintPass<'tcx> for Unwrap {
213-
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
224+
fn check_item(&mut self, _: &LateContext<'_>, item: &Item<'_>) {
214225
if is_test_module_or_function(item) {
215-
return;
226+
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
216227
}
217228
}
218229
fn check_fn(
@@ -224,19 +235,24 @@ impl<'tcx> LateLintPass<'tcx> for Unwrap {
224235
span: Span,
225236
fn_id: HirId,
226237
) {
227-
if span.from_expansion() {
228-
return;
238+
if_chain! {
239+
if !span.from_expansion();
240+
if !self.check_test_module();
241+
if let mut v = UnwrappableVariablesVisitor { cx, unwrappables: Vec::new(), };
242+
then {
243+
walk_fn(&mut v, kind, decl, body.id(), span, fn_id);
244+
}
245+
}
246+
}
247+
fn check_item_post(&mut self, _: &LateContext<'_>, item: &Item<'_>) {
248+
if is_test_module_or_function(item) {
249+
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
229250
}
230-
231-
let mut v = UnwrappableVariablesVisitor {
232-
cx,
233-
unwrappables: Vec::new(),
234-
};
235-
236-
walk_fn(&mut v, kind, decl, body.id(), span, fn_id);
237251
}
238252
}
239253

240-
fn is_test_module_or_function(item: &Item<'_>) -> bool {
241-
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
254+
impl Unwrap {
255+
fn check_test_module(&self) -> bool {
256+
self.test_modules_deep > 0
257+
}
242258
}

clippy_lints/src/utils/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,11 @@ pub fn is_slice_of_primitives(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
14161416
}
14171417
}
14181418

1419+
// Check if item is #[test]ing code.
1420+
pub fn is_test_module_or_function(item: &Item<'_>) -> bool {
1421+
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
1422+
}
1423+
14191424
#[macro_export]
14201425
macro_rules! unwrap_cargo_metadata {
14211426
($cx: ident, $lint: ident, $deps: expr) => {{

clippy_lints/src/wildcard_imports.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{in_macro, snippet, snippet_with_applicability, span_lint_and_sugg};
1+
use crate::utils::{in_macro, is_test_module_or_function, snippet, snippet_with_applicability, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{
@@ -208,7 +208,3 @@ fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
208208
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
209209
segments.len() == 1 && segments[0].ident.as_str() == "super"
210210
}
211-
212-
fn is_test_module_or_function(item: &Item<'_>) -> bool {
213-
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
214-
}

tests/ui/unwrap.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,7 @@ fn unwrap_result() {
1010
let _ = res.unwrap();
1111
}
1212

13-
// Should not get linted.
14-
#[test]
15-
fn with_test() {
16-
let opt = Some(0);
17-
let _ = opt.unwrap();
18-
}
19-
20-
// Should not get linted.
21-
#[cfg(test)]
22-
fn with_cfg_test() {
23-
let res: Result<u8, ()> = Ok(0);
24-
let _ = res.unwrap();
25-
}
26-
2713
fn main() {
2814
unwrap_option();
2915
unwrap_result();
30-
with_test();
31-
with_cfg_test();
3216
}

tests/ui/unwrap.stderr

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
error[E0425]: cannot find function `with_test` in this scope
2-
--> $DIR/unwrap.rs:30:5
1+
error: used `unwrap()` on `an Option` value
2+
--> $DIR/unwrap.rs:5:13
33
|
4-
LL | with_test();
5-
| ^^^^^^^^^ not found in this scope
4+
LL | let _ = opt.unwrap();
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::unwrap-used` implied by `-D warnings`
8+
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
69

7-
error[E0425]: cannot find function `with_cfg_test` in this scope
8-
--> $DIR/unwrap.rs:31:5
10+
error: used `unwrap()` on `a Result` value
11+
--> $DIR/unwrap.rs:10:13
12+
|
13+
LL | let _ = res.unwrap();
14+
| ^^^^^^^^^^^^
915
|
10-
LL | with_cfg_test();
11-
| ^^^^^^^^^^^^^ not found in this scope
16+
= help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message
1217

1318
error: aborting due to 2 previous errors
1419

15-
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)