]> git.lizzy.rs Git - rust.git/commitdiff
Make WhileTrue into an EarlyLintPass lint.
authorMazdak Farrokhzad <twingoow@gmail.com>
Fri, 21 Jun 2019 23:30:24 +0000 (01:30 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 6 Jul 2019 04:43:58 +0000 (06:43 +0200)
17 files changed:
src/librustc_lint/builtin.rs
src/librustc_lint/lib.rs
src/test/compile-fail/issue-52443.rs
src/test/ui/block-result/block-must-not-have-result-while.rs
src/test/ui/block-result/block-must-not-have-result-while.stderr
src/test/ui/borrowck/mut-borrow-in-loop.rs
src/test/ui/borrowck/mut-borrow-in-loop.stderr
src/test/ui/issues/issue-27042.rs
src/test/ui/issues/issue-27042.stderr
src/test/ui/lint/lint-impl-fn.stderr
src/test/ui/lint/lint-unnecessary-parens.rs
src/test/ui/lint/lint-unnecessary-parens.stderr
src/test/ui/lint/suggestions.stderr
src/test/ui/liveness/liveness-move-in-while.rs
src/test/ui/liveness/liveness-move-in-while.stderr
src/test/ui/loops/loop-break-value.rs
src/test/ui/loops/loop-break-value.stderr

index d98dfb5478a366eefad92ffb70e6b97593d73e69..c9605445c24014eac9a6d26c0eeb7d766ecb5a8c 100644 (file)
 
 declare_lint_pass!(WhileTrue => [WHILE_TRUE]);
 
-fn as_while_cond(expr: &hir::Expr) -> Option<&hir::Expr> {
-    if let hir::ExprKind::Loop(blk, ..) = &expr.node {
-        if let Some(match_expr) = &blk.expr {
-            if let hir::ExprKind::Match(cond, .., hir::MatchSource::WhileDesugar)
-                = &match_expr.node
-            {
-                if let hir::ExprKind::DropTemps(cond) = &cond.node {
-                    return Some(cond);
-                }
-            }
-        }
+/// Traverse through any amount of parenthesis and return the first non-parens expression.
+fn pierce_parens(mut expr: &ast::Expr) -> &ast::Expr {
+    while let ast::ExprKind::Paren(sub) = &expr.node {
+        expr = sub;
     }
-
-    None
+    expr
 }
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
-    fn check_expr(&mut self, cx: &LateContext<'_, '_>, e: &hir::Expr) {
-        if let Some(ref cond) = as_while_cond(e) {
-            if let hir::ExprKind::Lit(ref lit) = cond.node {
+impl EarlyLintPass for WhileTrue {
+    fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
+        if let ast::ExprKind::While(cond, ..) = &e.node {
+            if let ast::ExprKind::Lit(ref lit) = pierce_parens(cond).node {
                 if let ast::LitKind::Bool(true) = lit.node {
                     if lit.span.ctxt() == SyntaxContext::empty() {
                         let msg = "denote infinite loops with `loop { ... }`";
-                        let condition_span = cx.tcx.sess.source_map().def_span(e.span);
-                        let mut err = cx.struct_span_lint(WHILE_TRUE, condition_span, msg);
-                        err.span_suggestion_short(
-                            condition_span,
-                            "use `loop`",
-                            "loop".to_owned(),
-                            Applicability::MachineApplicable
-                        );
-                        err.emit();
+                        let condition_span = cx.sess.source_map().def_span(e.span);
+                        cx.struct_span_lint(WHILE_TRUE, condition_span, msg)
+                            .span_suggestion_short(
+                                condition_span,
+                                "use `loop`",
+                                "loop".to_owned(),
+                                Applicability::MachineApplicable
+                            )
+                            .emit();
                     }
                 }
             }
index fb02782e6d3200fca67125d7dc978ca5bd3aa765..2519981fa21b2e021a2961678f1524ad7027d042 100644 (file)
@@ -96,6 +96,7 @@ macro_rules! early_lint_passes {
             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
             NonCamelCaseTypes: NonCamelCaseTypes,
             DeprecatedAttr: DeprecatedAttr::new(),
+            WhileTrue: WhileTrue,
         ]);
     )
 }
@@ -140,7 +141,6 @@ macro_rules! late_lint_mod_passes {
     ($macro:path, $args:tt) => (
         $macro!($args, [
             HardwiredLints: HardwiredLints,
-            WhileTrue: WhileTrue,
             ImproperCTypes: ImproperCTypes,
             VariantSizeDifferences: VariantSizeDifferences,
             BoxPointers: BoxPointers,
index 0d6906086e9b1f24563b123336004502bda38846..a993d811d327f851f95f082107b35dac12f7eea4 100644 (file)
@@ -2,6 +2,7 @@ fn main() {
     [(); & { loop { continue } } ]; //~ ERROR mismatched types
     [(); loop { break }]; //~ ERROR mismatched types
     [(); {while true {break}; 0}]; //~ ERROR constant contains unimplemented expression type
+    //~^ WARN denote infinite loops with
     [(); { for _ in 0usize.. {}; 0}]; //~ ERROR calls in constants are limited to constant functions
     //~^ ERROR constant contains unimplemented expression type
     //~| ERROR constant contains unimplemented expression type
index 96597631396df55a10ce75185a93174df68a83a7..108b9bc9e9b29c9adfddb81d48dd3e32051d1ab4 100644 (file)
@@ -1,5 +1,5 @@
 fn main() {
-    while true {
+    while true { //~ WARN denote infinite loops with
         true //~  ERROR mismatched types
              //~| expected type `()`
              //~| found type `bool`
index 302d2972f7de14629551b7429e901641492f7804..c41afcc9121c697bf1e248e65ddce2ce9a860099 100644 (file)
@@ -1,3 +1,11 @@
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/block-must-not-have-result-while.rs:2:5
+   |
+LL |     while true {
+   |     ^^^^^^^^^^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
 error[E0308]: mismatched types
   --> $DIR/block-must-not-have-result-while.rs:3:9
    |
index 09f3e4f9b76d86d4bd9a73eb9ef4ec6e6944f7a8..22667906e12d6c546e50552042a54b966fc54b51 100644 (file)
@@ -12,7 +12,7 @@ fn in_loop(self, arg : &'a mut T) {
     }
 
     fn in_while(self, arg : &'a mut T) {
-        while true {
+        while true { //~ WARN denote infinite loops with
             (self.func)(arg) //~ ERROR cannot borrow
         }
     }
index eda2f518f9246affc691ef46367ef36413c1e49a..59cf4d533def8d760369bb9dd98ffc08356a5524 100644 (file)
@@ -1,3 +1,11 @@
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/mut-borrow-in-loop.rs:15:9
+   |
+LL |         while true {
+   |         ^^^^^^^^^^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
 error[E0499]: cannot borrow `*arg` as mutable more than once at a time
   --> $DIR/mut-borrow-in-loop.rs:10:25
    |
index 8c7758597f03e79c554b1df3beb7b34fd10d8462..517c1f2e6c7cc2b4cbb1b073fc9d5d6390450fa0 100644 (file)
@@ -6,6 +6,7 @@ fn main() {
         loop { break }; //~ ERROR mismatched types
     let _: i32 =
         'b: //~ ERROR mismatched types
+        //~^ WARN denote infinite loops with
         while true { break }; // but here we cite the whole loop
     let _: i32 =
         'c: //~ ERROR mismatched types
index cce7d24a5f602d7d6ac9676094180cf00203c432..c67b8ad738155533d0854284a57dfc619f66e623 100644 (file)
@@ -1,3 +1,13 @@
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/issue-27042.rs:8:9
+   |
+LL | /         'b:
+LL | |
+LL | |         while true { break }; // but here we cite the whole loop
+   | |____________________________^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
 error[E0308]: mismatched types
   --> $DIR/issue-27042.rs:6:16
    |
@@ -11,6 +21,7 @@ error[E0308]: mismatched types
   --> $DIR/issue-27042.rs:8:9
    |
 LL | /         'b:
+LL | |
 LL | |         while true { break }; // but here we cite the whole loop
    | |____________________________^ expected i32, found ()
    |
@@ -18,7 +29,7 @@ LL | |         while true { break }; // but here we cite the whole loop
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/issue-27042.rs:11:9
+  --> $DIR/issue-27042.rs:12:9
    |
 LL | /         'c:
 LL | |         for _ in None { break }; // but here we cite the whole loop
@@ -28,7 +39,7 @@ LL | |         for _ in None { break }; // but here we cite the whole loop
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/issue-27042.rs:14:9
+  --> $DIR/issue-27042.rs:15:9
    |
 LL | /         'd:
 LL | |         while let Some(_) = None { break };
index 56f85111d428f9678787b045aa51d535c0f15b39..2c9a264287c9675e62d8af6d216d0f39b29c7a18 100644 (file)
@@ -11,25 +11,25 @@ LL |     #[deny(while_true)]
    |            ^^^^^^^^^^
 
 error: denote infinite loops with `loop { ... }`
-  --> $DIR/lint-impl-fn.rs:27:5
+  --> $DIR/lint-impl-fn.rs:18:25
    |
-LL |     while true {}
-   |     ^^^^^^^^^^ help: use `loop`
+LL |         fn foo(&self) { while true {} }
+   |                         ^^^^^^^^^^ help: use `loop`
    |
 note: lint level defined here
-  --> $DIR/lint-impl-fn.rs:25:8
+  --> $DIR/lint-impl-fn.rs:13:8
    |
 LL | #[deny(while_true)]
    |        ^^^^^^^^^^
 
 error: denote infinite loops with `loop { ... }`
-  --> $DIR/lint-impl-fn.rs:18:25
+  --> $DIR/lint-impl-fn.rs:27:5
    |
-LL |         fn foo(&self) { while true {} }
-   |                         ^^^^^^^^^^ help: use `loop`
+LL |     while true {}
+   |     ^^^^^^^^^^ help: use `loop`
    |
 note: lint level defined here
-  --> $DIR/lint-impl-fn.rs:13:8
+  --> $DIR/lint-impl-fn.rs:25:8
    |
 LL | #[deny(while_true)]
    |        ^^^^^^^^^^
index c36101043a7f711b2bee404aace082a7753bd45a..811bc87eb0e2eee5afe81311b39301f29fa79d37 100644 (file)
@@ -19,6 +19,7 @@ fn main() {
 
     if (true) {} //~ ERROR unnecessary parentheses around `if` condition
     while (true) {} //~ ERROR unnecessary parentheses around `while` condition
+    //~^ WARN denote infinite loops with
     match (true) { //~ ERROR unnecessary parentheses around `match` head expression
         _ => {}
     }
index dfbefd7b03ee5338d6e76a265a5f95400ee0c27d..05ecbfdf4fa39a173a6e976f949a5ed944f8638b 100644 (file)
@@ -34,44 +34,52 @@ error: unnecessary parentheses around `while` condition
 LL |     while (true) {}
    |           ^^^^^^ help: remove these parentheses
 
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/lint-unnecessary-parens.rs:21:5
+   |
+LL |     while (true) {}
+   |     ^^^^^^^^^^^^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
 error: unnecessary parentheses around `match` head expression
-  --> $DIR/lint-unnecessary-parens.rs:22:11
+  --> $DIR/lint-unnecessary-parens.rs:23:11
    |
 LL |     match (true) {
    |           ^^^^^^ help: remove these parentheses
 
 error: unnecessary parentheses around `let` head expression
-  --> $DIR/lint-unnecessary-parens.rs:25:16
+  --> $DIR/lint-unnecessary-parens.rs:26:16
    |
 LL |     if let 1 = (1) {}
    |                ^^^ help: remove these parentheses
 
 error: unnecessary parentheses around `let` head expression
-  --> $DIR/lint-unnecessary-parens.rs:26:19
+  --> $DIR/lint-unnecessary-parens.rs:27:19
    |
 LL |     while let 1 = (2) {}
    |                   ^^^ help: remove these parentheses
 
 error: unnecessary parentheses around method argument
-  --> $DIR/lint-unnecessary-parens.rs:40:24
+  --> $DIR/lint-unnecessary-parens.rs:41:24
    |
 LL |     X { y: false }.foo((true));
    |                        ^^^^^^ help: remove these parentheses
 
 error: unnecessary parentheses around assigned value
-  --> $DIR/lint-unnecessary-parens.rs:42:18
+  --> $DIR/lint-unnecessary-parens.rs:43:18
    |
 LL |     let mut _a = (0);
    |                  ^^^ help: remove these parentheses
 
 error: unnecessary parentheses around assigned value
-  --> $DIR/lint-unnecessary-parens.rs:43:10
+  --> $DIR/lint-unnecessary-parens.rs:44:10
    |
 LL |     _a = (0);
    |          ^^^ help: remove these parentheses
 
 error: unnecessary parentheses around assigned value
-  --> $DIR/lint-unnecessary-parens.rs:44:11
+  --> $DIR/lint-unnecessary-parens.rs:45:11
    |
 LL |     _a += (1);
    |           ^^^ help: remove these parentheses
index 5aaa9947f998a73e80a637f11a0faf47a75cb62a..de7c1fb898679e0ea5fde2850739b3967b9b7f7e 100644 (file)
@@ -1,3 +1,11 @@
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/suggestions.rs:46:5
+   |
+LL |     while true {
+   |     ^^^^^^^^^^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
 warning: unnecessary parentheses around assigned value
   --> $DIR/suggestions.rs:49:31
    |
@@ -65,14 +73,6 @@ LL | pub fn defiant<T>(_t: T) {}
    |
    = note: #[warn(no_mangle_generic_items)] on by default
 
-warning: denote infinite loops with `loop { ... }`
-  --> $DIR/suggestions.rs:46:5
-   |
-LL |     while true {
-   |     ^^^^^^^^^^ help: use `loop`
-   |
-   = note: #[warn(while_true)] on by default
-
 warning: the `warp_factor:` in this pattern is redundant
   --> $DIR/suggestions.rs:61:23
    |
index 420d1311f8b1f929ead8f88f4eb8d212be0fbbb1..9f3ebf1362b823fc736e10d3999584a826e2a5dd 100644 (file)
@@ -6,5 +6,8 @@ fn main() {
     loop {
         println!("{}", y); //~ ERROR borrow of moved value: `y`
         while true { while true { while true { x = y; x.clone(); } } }
+        //~^ WARN denote infinite loops with
+        //~| WARN denote infinite loops with
+        //~| WARN denote infinite loops with
     }
 }
index e1eed1b59f47001a78282bfc4459ad9e3cdbb122..bbf5e50f1e09d3662a259cacf0aab17f6f5c1e39 100644 (file)
@@ -1,3 +1,23 @@
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/liveness-move-in-while.rs:8:9
+   |
+LL |         while true { while true { while true { x = y; x.clone(); } } }
+   |         ^^^^^^^^^^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/liveness-move-in-while.rs:8:22
+   |
+LL |         while true { while true { while true { x = y; x.clone(); } } }
+   |                      ^^^^^^^^^^ help: use `loop`
+
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/liveness-move-in-while.rs:8:35
+   |
+LL |         while true { while true { while true { x = y; x.clone(); } } }
+   |                                   ^^^^^^^^^^ help: use `loop`
+
 error[E0382]: borrow of moved value: `y`
   --> $DIR/liveness-move-in-while.rs:7:24
    |
index b80c847deb96708b4cba778eb7e6acf81c2c0d3a..7c2f63ec51a79d0bf22f3241df642b86511cadd9 100644 (file)
@@ -23,7 +23,7 @@ fn main() {
         };
     };
 
-    'while_loop: while true {
+    'while_loop: while true { //~ WARN denote infinite loops with
         break;
         break (); //~ ERROR `break` with value from a `while` loop
         loop {
index 13fe50855408bd3b24b69f46854bffe09a515b42..f458c88ea4892989b7fac8a14b94ee6adf510578 100644 (file)
@@ -1,3 +1,11 @@
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/loop-break-value.rs:26:5
+   |
+LL |     'while_loop: while true {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
+   |
+   = note: #[warn(while_true)] on by default
+
 error[E0571]: `break` with value from a `while` loop
   --> $DIR/loop-break-value.rs:28:9
    |