about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCameron Steffen <cam.steffen94@gmail.com>2022-02-08 10:45:52 -0600
committerCameron Steffen <cam.steffen94@gmail.com>2022-02-08 15:11:48 -0600
commit88fd0905315ec02cadb224d034cd017b6ffcd379 (patch)
treea3a2320af49239701182c6b56f0ad3c5ab195272
parentcaeebd6b095d12f657cc1a9f176d5c9e1e372b21 (diff)
downloadrust-88fd0905315ec02cadb224d034cd017b6ffcd379.tar.gz
rust-88fd0905315ec02cadb224d034cd017b6ffcd379.zip
Factor out ui_test suite
-rw-r--r--clippy_utils/src/lib.rs4
-rw-r--r--tests/compile-test.rs9
-rw-r--r--tests/ui/eq_op.rs93
-rw-r--r--tests/ui/eq_op.stderr162
-rw-r--r--tests/ui_test/eq_op.rs15
5 files changed, 134 insertions, 149 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index f76004774ad..42955080c96 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -2116,7 +2116,7 @@ fn with_test_item_names<'tcx>(tcx: TyCtxt<'tcx>, module: LocalDefId, f: impl Fn(
 
 /// Checks if the function containing the given `HirId` is a `#[test]` function
 ///
-/// Note: If you use this function, please add a `#[test]` case in `tests/ui_test`.
+/// Note: Add `// compile-flags: --test` to UI tests with a `#[test]` function
 pub fn is_in_test_function(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
     with_test_item_names(tcx, tcx.parent_module(id), |names| {
         tcx.hir()
@@ -2139,7 +2139,7 @@ pub fn is_in_test_function(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
 /// Checks whether item either has `test` attribute applied, or
 /// is a module with `test` in its name.
 ///
-/// Note: If you use this function, please add a `#[test]` case in `tests/ui_test`.
+/// Note: Add `// compile-flags: --test` to UI tests with a `#[test]` function
 pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
     is_in_test_function(tcx, item.hir_id())
         || matches!(item.kind, ItemKind::Mod(..))
diff --git a/tests/compile-test.rs b/tests/compile-test.rs
index ab7e2540405..a82ff182839 100644
--- a/tests/compile-test.rs
+++ b/tests/compile-test.rs
@@ -165,14 +165,6 @@ fn run_ui() {
     compiletest::run_tests(&config);
 }
 
-fn run_ui_test() {
-    let mut config = base_config("ui_test");
-    let _g = VarGuard::set("CARGO_MANIFEST_DIR", fs::canonicalize("tests").unwrap());
-    let rustcflags = config.target_rustcflags.get_or_insert_with(Default::default);
-    rustcflags.push_str(" --test");
-    compiletest::run_tests(&config);
-}
-
 fn run_internal_tests() {
     // only run internal tests with the internal-tests feature
     if !RUN_INTERNAL_TESTS {
@@ -328,7 +320,6 @@ fn run_ui_cargo() {
 fn compile_test() {
     set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
     run_ui();
-    run_ui_test();
     run_ui_toml();
     run_ui_cargo();
     run_internal_tests();
diff --git a/tests/ui/eq_op.rs b/tests/ui/eq_op.rs
index 707b449f82e..422f9486503 100644
--- a/tests/ui/eq_op.rs
+++ b/tests/ui/eq_op.rs
@@ -1,58 +1,56 @@
-// does not test any rustfixable lints
-
-#[rustfmt::skip]
-#[warn(clippy::eq_op)]
-#[allow(clippy::identity_op, clippy::double_parens)]
-#[allow(clippy::no_effect, unused_variables, clippy::unnecessary_operation, clippy::short_circuit_statement)]
-#[allow(clippy::nonminimal_bool)]
-#[allow(unused)]
-#[allow(clippy::unnecessary_cast)]
+// compile-flags: --test
+
+#![warn(clippy::eq_op)]
+#![allow(clippy::double_parens, clippy::identity_op, clippy::nonminimal_bool)]
+
 fn main() {
     // simple values and comparisons
-    1 == 1;
-    "no" == "no";
+    let _ = 1 == 1;
+    let _ = "no" == "no";
     // even though I agree that no means no ;-)
-    false != false;
-    1.5 < 1.5;
-    1u64 >= 1u64;
+    let _ = false != false;
+    let _ = 1.5 < 1.5;
+    let _ = 1u64 >= 1u64;
 
     // casts, methods, parentheses
-    (1 as u64) & (1 as u64);
-    1 ^ ((((((1))))));
+    let _ = (1u32 as u64) & (1u32 as u64);
+    #[rustfmt::skip]
+    {
+        let _ = 1 ^ ((((((1))))));
+    };
 
     // unary and binary operators
-    (-(2) < -(2));
-    ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
-    (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
+    let _ = (-(2) < -(2));
+    let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
+    let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
 
     // various other things
-    ([1] != [1]);
-    ((1, 2) != (1, 2));
-    vec![1, 2, 3] == vec![1, 2, 3]; //no error yet, as we don't match macros
+    let _ = ([1] != [1]);
+    let _ = ((1, 2) != (1, 2));
+    let _ = vec![1, 2, 3] == vec![1, 2, 3]; //no error yet, as we don't match macros
 
     // const folding
-    1 + 1 == 2;
-    1 - 1 == 0;
+    let _ = 1 + 1 == 2;
+    let _ = 1 - 1 == 0;
 
-    1 - 1;
-    1 / 1;
-    true && true;
-
-    true || true;
+    let _ = 1 - 1;
+    let _ = 1 / 1;
+    let _ = true && true;
 
+    let _ = true || true;
 
     let a: u32 = 0;
     let b: u32 = 0;
 
-    a == b && b == a;
-    a != b && b != a;
-    a < b && b > a;
-    a <= b && b >= a;
+    let _ = a == b && b == a;
+    let _ = a != b && b != a;
+    let _ = a < b && b > a;
+    let _ = a <= b && b >= a;
 
     let mut a = vec![1];
-    a == a;
-    2*a.len() == 2*a.len(); // ok, functions
-    a.pop() == a.pop(); // ok, functions
+    let _ = a == a;
+    let _ = 2 * a.len() == 2 * a.len(); // ok, functions
+    let _ = a.pop() == a.pop(); // ok, functions
 
     check_ignore_macro();
 
@@ -63,15 +61,14 @@ fn main() {
     const D: u32 = A / A;
 }
 
-#[rustfmt::skip]
 macro_rules! check_if_named_foo {
-    ($expression:expr) => (
+    ($expression:expr) => {
         if stringify!($expression) == "foo" {
             println!("foo!");
         } else {
             println!("not foo.");
         }
-    )
+    };
 }
 
 macro_rules! bool_macro {
@@ -80,11 +77,10 @@ macro_rules! bool_macro {
     };
 }
 
-#[allow(clippy::short_circuit_statement)]
 fn check_ignore_macro() {
     check_if_named_foo!(foo);
     // checks if the lint ignores macros with `!` operator
-    !bool_macro!(1) && !bool_macro!("");
+    let _ = !bool_macro!(1) && !bool_macro!("");
 }
 
 struct Nested {
@@ -95,3 +91,18 @@ fn check_nested(n1: &Nested, n2: &Nested) -> bool {
     // `n2.inner.0.0` mistyped as `n1.inner.0.0`
     (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0
 }
+
+#[test]
+fn eq_op_shouldnt_trigger_in_tests() {
+    let a = 1;
+    let result = a + 1 == 1 + a;
+    assert!(result);
+}
+
+#[test]
+fn eq_op_macros_shouldnt_trigger_in_tests() {
+    let a = 1;
+    let b = 2;
+    assert_eq!(a, a);
+    assert_eq!(a + b, b + a);
+}
diff --git a/tests/ui/eq_op.stderr b/tests/ui/eq_op.stderr
index 8ef658af8df..313ceed2b41 100644
--- a/tests/ui/eq_op.stderr
+++ b/tests/ui/eq_op.stderr
@@ -1,174 +1,172 @@
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:12:5
+  --> $DIR/eq_op.rs:8:13
    |
-LL |     1 == 1;
-   |     ^^^^^^
+LL |     let _ = 1 == 1;
+   |             ^^^^^^
    |
    = note: `-D clippy::eq-op` implied by `-D warnings`
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:13:5
+  --> $DIR/eq_op.rs:9:13
    |
-LL |     "no" == "no";
-   |     ^^^^^^^^^^^^
+LL |     let _ = "no" == "no";
+   |             ^^^^^^^^^^^^
 
 error: equal expressions as operands to `!=`
-  --> $DIR/eq_op.rs:15:5
+  --> $DIR/eq_op.rs:11:13
    |
-LL |     false != false;
-   |     ^^^^^^^^^^^^^^
+LL |     let _ = false != false;
+   |             ^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `<`
-  --> $DIR/eq_op.rs:16:5
+  --> $DIR/eq_op.rs:12:13
    |
-LL |     1.5 < 1.5;
-   |     ^^^^^^^^^
+LL |     let _ = 1.5 < 1.5;
+   |             ^^^^^^^^^
 
 error: equal expressions as operands to `>=`
-  --> $DIR/eq_op.rs:17:5
+  --> $DIR/eq_op.rs:13:13
    |
-LL |     1u64 >= 1u64;
-   |     ^^^^^^^^^^^^
+LL |     let _ = 1u64 >= 1u64;
+   |             ^^^^^^^^^^^^
 
 error: equal expressions as operands to `&`
-  --> $DIR/eq_op.rs:20:5
+  --> $DIR/eq_op.rs:16:13
    |
-LL |     (1 as u64) & (1 as u64);
-   |     ^^^^^^^^^^^^^^^^^^^^^^^
+LL |     let _ = (1u32 as u64) & (1u32 as u64);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `^`
-  --> $DIR/eq_op.rs:21:5
+  --> $DIR/eq_op.rs:19:17
    |
-LL |     1 ^ ((((((1))))));
-   |     ^^^^^^^^^^^^^^^^^
+LL |         let _ = 1 ^ ((((((1))))));
+   |                 ^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `<`
-  --> $DIR/eq_op.rs:24:5
+  --> $DIR/eq_op.rs:23:13
    |
-LL |     (-(2) < -(2));
-   |     ^^^^^^^^^^^^^
+LL |     let _ = (-(2) < -(2));
+   |             ^^^^^^^^^^^^^
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:25:5
+  --> $DIR/eq_op.rs:24:13
    |
-LL |     ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |     let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `&`
-  --> $DIR/eq_op.rs:25:6
+  --> $DIR/eq_op.rs:24:14
    |
-LL |     ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
-   |      ^^^^^^^^^^^^^^^^^
+LL |     let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
+   |              ^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `&`
-  --> $DIR/eq_op.rs:25:27
+  --> $DIR/eq_op.rs:24:35
    |
-LL |     ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
-   |                           ^^^^^^^^^^^^^^^^^
+LL |     let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
+   |                                   ^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:26:5
+  --> $DIR/eq_op.rs:25:13
    |
-LL |     (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |     let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `!=`
-  --> $DIR/eq_op.rs:29:5
+  --> $DIR/eq_op.rs:28:13
    |
-LL |     ([1] != [1]);
-   |     ^^^^^^^^^^^^
+LL |     let _ = ([1] != [1]);
+   |             ^^^^^^^^^^^^
 
 error: equal expressions as operands to `!=`
-  --> $DIR/eq_op.rs:30:5
+  --> $DIR/eq_op.rs:29:13
    |
-LL |     ((1, 2) != (1, 2));
-   |     ^^^^^^^^^^^^^^^^^^
+LL |     let _ = ((1, 2) != (1, 2));
+   |             ^^^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:34:5
+  --> $DIR/eq_op.rs:33:13
    |
-LL |     1 + 1 == 2;
-   |     ^^^^^^^^^^
+LL |     let _ = 1 + 1 == 2;
+   |             ^^^^^^^^^^
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:35:5
+  --> $DIR/eq_op.rs:34:13
    |
-LL |     1 - 1 == 0;
-   |     ^^^^^^^^^^
+LL |     let _ = 1 - 1 == 0;
+   |             ^^^^^^^^^^
 
 error: equal expressions as operands to `-`
-  --> $DIR/eq_op.rs:35:5
+  --> $DIR/eq_op.rs:34:13
    |
-LL |     1 - 1 == 0;
-   |     ^^^^^
+LL |     let _ = 1 - 1 == 0;
+   |             ^^^^^
 
 error: equal expressions as operands to `-`
-  --> $DIR/eq_op.rs:37:5
+  --> $DIR/eq_op.rs:36:13
    |
-LL |     1 - 1;
-   |     ^^^^^
+LL |     let _ = 1 - 1;
+   |             ^^^^^
 
 error: equal expressions as operands to `/`
-  --> $DIR/eq_op.rs:38:5
+  --> $DIR/eq_op.rs:37:13
    |
-LL |     1 / 1;
-   |     ^^^^^
+LL |     let _ = 1 / 1;
+   |             ^^^^^
 
 error: equal expressions as operands to `&&`
-  --> $DIR/eq_op.rs:39:5
+  --> $DIR/eq_op.rs:38:13
    |
-LL |     true && true;
-   |     ^^^^^^^^^^^^
+LL |     let _ = true && true;
+   |             ^^^^^^^^^^^^
 
 error: equal expressions as operands to `||`
-  --> $DIR/eq_op.rs:41:5
+  --> $DIR/eq_op.rs:40:13
    |
-LL |     true || true;
-   |     ^^^^^^^^^^^^
+LL |     let _ = true || true;
+   |             ^^^^^^^^^^^^
 
 error: equal expressions as operands to `&&`
-  --> $DIR/eq_op.rs:47:5
+  --> $DIR/eq_op.rs:45:13
    |
-LL |     a == b && b == a;
-   |     ^^^^^^^^^^^^^^^^
+LL |     let _ = a == b && b == a;
+   |             ^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `&&`
-  --> $DIR/eq_op.rs:48:5
+  --> $DIR/eq_op.rs:46:13
    |
-LL |     a != b && b != a;
-   |     ^^^^^^^^^^^^^^^^
+LL |     let _ = a != b && b != a;
+   |             ^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `&&`
-  --> $DIR/eq_op.rs:49:5
+  --> $DIR/eq_op.rs:47:13
    |
-LL |     a < b && b > a;
-   |     ^^^^^^^^^^^^^^
+LL |     let _ = a < b && b > a;
+   |             ^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `&&`
-  --> $DIR/eq_op.rs:50:5
+  --> $DIR/eq_op.rs:48:13
    |
-LL |     a <= b && b >= a;
-   |     ^^^^^^^^^^^^^^^^
+LL |     let _ = a <= b && b >= a;
+   |             ^^^^^^^^^^^^^^^^
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:53:5
+  --> $DIR/eq_op.rs:51:13
    |
-LL |     a == a;
-   |     ^^^^^^
+LL |     let _ = a == a;
+   |             ^^^^^^
 
 error: equal expressions as operands to `/`
-  --> $DIR/eq_op.rs:63:20
+  --> $DIR/eq_op.rs:61:20
    |
 LL |     const D: u32 = A / A;
    |                    ^^^^^
 
 error: equal expressions as operands to `==`
-  --> $DIR/eq_op.rs:96:5
+  --> $DIR/eq_op.rs:92:5
    |
 LL |     (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-   = note: `#[deny(clippy::eq_op)]` on by default
 
 error: aborting due to 28 previous errors
 
diff --git a/tests/ui_test/eq_op.rs b/tests/ui_test/eq_op.rs
deleted file mode 100644
index f2f5f1e588e..00000000000
--- a/tests/ui_test/eq_op.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-#[warn(clippy::eq_op)]
-#[test]
-fn eq_op_shouldnt_trigger_in_tests() {
-    let a = 1;
-    let result = a + 1 == 1 + a;
-    assert!(result);
-}
-
-#[test]
-fn eq_op_macros_shouldnt_trigger_in_tests() {
-    let a = 1;
-    let b = 2;
-    assert_eq!(a, a);
-    assert_eq!(a + b, b + a);
-}