From 6a2003e4e30e368ce89a76efc4f6dbf824d01511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 8 Jun 2018 18:24:57 -0700 Subject: [PATCH 1/2] Lint inner fn marked as `#[test]` --- src/librustc_lint/builtin.rs | 42 ++++++++++++++++++++++++++- src/librustc_lint/lib.rs | 1 + src/test/ui/lint/test-inner-fn.rs | 29 ++++++++++++++++++ src/test/ui/lint/test-inner-fn.stderr | 16 ++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/lint/test-inner-fn.rs create mode 100644 src/test/ui/lint/test-inner-fn.stderr diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index dfbfcfccf7c89..28e59237854c5 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1704,7 +1704,6 @@ impl LintPass for SoftLints { } } - declare_lint! { pub ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, Allow, @@ -1739,3 +1738,44 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns { } } } + +declare_lint! { + UNTESTABLE_METHOD, + Warn, + "detects untestable method marked as #[test]" +} + +pub struct UntestableMethod; + +impl LintPass for UntestableMethod { + fn get_lints(&self) -> LintArray { + lint_array!(UNTESTABLE_METHOD) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UntestableMethod { + fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { + match it.node { + hir::ItemFn(..) => { + for attr in &it.attrs { + if attr.name() == "test" { + let parent = cx.tcx.hir.get_parent(it.id); + match cx.tcx.hir.find(parent) { + Some(hir_map::NodeItem(hir::Item {node: hir::ItemMod(_), ..})) | + None => {} + _ => { + cx.struct_span_lint( + UNTESTABLE_METHOD, + attr.span, + "cannot test inner function", + ).emit(); + } + } + break; + } + } + } + _ => return, + }; + } +} diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 13e97a9d3e591..6c39af6dad2e3 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -130,6 +130,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { MutableTransmutes: MutableTransmutes, UnionsWithDropFields: UnionsWithDropFields, UnreachablePub: UnreachablePub, + UntestableMethod: UntestableMethod, TypeAliasBounds: TypeAliasBounds, UnusedBrokenConst: UnusedBrokenConst, TrivialConstraints: TrivialConstraints, diff --git a/src/test/ui/lint/test-inner-fn.rs b/src/test/ui/lint/test-inner-fn.rs new file mode 100644 index 0000000000000..112cc2d76723e --- /dev/null +++ b/src/test/ui/lint/test-inner-fn.rs @@ -0,0 +1,29 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --test -D untestable_method + +#[test] +fn foo() { + #[test] //~ ERROR cannot test inner function [untestable_method] + fn bar() {} + bar(); +} + +mod x { + #[test] + fn foo() { + #[test] //~ ERROR cannot test inner function [untestable_method] + fn bar() {} + bar(); + } +} + +fn main() {} diff --git a/src/test/ui/lint/test-inner-fn.stderr b/src/test/ui/lint/test-inner-fn.stderr new file mode 100644 index 0000000000000..a153f11e4cfdd --- /dev/null +++ b/src/test/ui/lint/test-inner-fn.stderr @@ -0,0 +1,16 @@ +error: cannot test inner function + --> $DIR/test-inner-fn.rs:15:5 + | +LL | #[test] //~ ERROR cannot test inner function [untestable_method] + | ^^^^^^^ + | + = note: requested on the command line with `-D untestable-method` + +error: cannot test inner function + --> $DIR/test-inner-fn.rs:23:9 + | +LL | #[test] //~ ERROR cannot test inner function [untestable_method] + | ^^^^^^^ + +error: aborting due to 2 previous errors + From 51a0425f36f313a08a6925612e772c7302525991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 19 Jun 2018 16:03:31 -0700 Subject: [PATCH 2/2] rename lint to `unnameable-test-functions` --- src/librustc_lint/builtin.rs | 14 +++++++------- src/librustc_lint/lib.rs | 2 +- src/test/ui/lint/test-inner-fn.rs | 6 +++--- src/test/ui/lint/test-inner-fn.stderr | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 28e59237854c5..196290d495c77 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1740,20 +1740,20 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns { } declare_lint! { - UNTESTABLE_METHOD, + UNNAMEABLE_TEST_FUNCTIONS, Warn, - "detects untestable method marked as #[test]" + "detects an function that cannot be named being marked as #[test]" } -pub struct UntestableMethod; +pub struct UnnameableTestFunctions; -impl LintPass for UntestableMethod { +impl LintPass for UnnameableTestFunctions { fn get_lints(&self) -> LintArray { - lint_array!(UNTESTABLE_METHOD) + lint_array!(UNNAMEABLE_TEST_FUNCTIONS) } } -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UntestableMethod { +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestFunctions { fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { match it.node { hir::ItemFn(..) => { @@ -1765,7 +1765,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UntestableMethod { None => {} _ => { cx.struct_span_lint( - UNTESTABLE_METHOD, + UNNAMEABLE_TEST_FUNCTIONS, attr.span, "cannot test inner function", ).emit(); diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 6c39af6dad2e3..adc700506ffc0 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -130,7 +130,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { MutableTransmutes: MutableTransmutes, UnionsWithDropFields: UnionsWithDropFields, UnreachablePub: UnreachablePub, - UntestableMethod: UntestableMethod, + UnnameableTestFunctions: UnnameableTestFunctions, TypeAliasBounds: TypeAliasBounds, UnusedBrokenConst: UnusedBrokenConst, TrivialConstraints: TrivialConstraints, diff --git a/src/test/ui/lint/test-inner-fn.rs b/src/test/ui/lint/test-inner-fn.rs index 112cc2d76723e..4304c96197f96 100644 --- a/src/test/ui/lint/test-inner-fn.rs +++ b/src/test/ui/lint/test-inner-fn.rs @@ -8,11 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-flags: --test -D untestable_method +// compile-flags: --test -D unnameable_test_functions #[test] fn foo() { - #[test] //~ ERROR cannot test inner function [untestable_method] + #[test] //~ ERROR cannot test inner function [unnameable_test_functions] fn bar() {} bar(); } @@ -20,7 +20,7 @@ fn foo() { mod x { #[test] fn foo() { - #[test] //~ ERROR cannot test inner function [untestable_method] + #[test] //~ ERROR cannot test inner function [unnameable_test_functions] fn bar() {} bar(); } diff --git a/src/test/ui/lint/test-inner-fn.stderr b/src/test/ui/lint/test-inner-fn.stderr index a153f11e4cfdd..37f0c161036ee 100644 --- a/src/test/ui/lint/test-inner-fn.stderr +++ b/src/test/ui/lint/test-inner-fn.stderr @@ -1,15 +1,15 @@ error: cannot test inner function --> $DIR/test-inner-fn.rs:15:5 | -LL | #[test] //~ ERROR cannot test inner function [untestable_method] +LL | #[test] //~ ERROR cannot test inner function [unnameable_test_functions] | ^^^^^^^ | - = note: requested on the command line with `-D untestable-method` + = note: requested on the command line with `-D unnameable-test-functions` error: cannot test inner function --> $DIR/test-inner-fn.rs:23:9 | -LL | #[test] //~ ERROR cannot test inner function [untestable_method] +LL | #[test] //~ ERROR cannot test inner function [unnameable_test_functions] | ^^^^^^^ error: aborting due to 2 previous errors