Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / frontend / simple_misc_analysis.nit
index 26d6710..4934e1a 100644 (file)
@@ -24,6 +24,7 @@ module simple_misc_analysis
 import phase
 
 redef class ToolContext
+       # Execute `AModule::do_simple_misc_analysis` on each module.
        var simple_misc_analysis_phase: Phase = new SimpleMiscAnalysisPhase(self, null)
 end
 
@@ -39,6 +40,12 @@ redef class AModule
        do
                var v = new SimpleMiscVisitor(toolcontext)
                v.enter_visit(self)
+
+               var t = location.file.first_token
+               while t != null do
+                       t.accept_simple_misc_token(v)
+                       t = t.next_token
+               end
        end
 end
 
@@ -54,14 +61,17 @@ private class SimpleMiscVisitor
 
        var toolcontext: ToolContext
 
-       fun warning(node: ANode, msg: String)
+       fun warning(node: ANode, tag, msg: String)
        do
-               toolcontext.warning(node.hot_location, msg)
+               toolcontext.warning(node.hot_location, tag, msg)
        end
 
-       init(toolcontext: ToolContext)
+       # Issue a warning if `sub` is a standalone `do` block.
+       fun check_do_expr(sub: nullable AExpr)
        do
-               self.toolcontext = toolcontext
+               if sub isa ADoExpr then
+                       warning(sub, "useless-do", "Warning: superfluous `do` block.")
+               end
        end
 end
 
@@ -77,11 +87,17 @@ redef class ANode
        private fun after_simple_misc(v: SimpleMiscVisitor) do end
 end
 
+redef class Token
+       private fun accept_simple_misc_token(v: SimpleMiscVisitor)
+       do
+       end
+end
+
 redef class ASignature
        redef fun after_simple_misc(v)
        do
                if self.n_opar != null and self.n_params.is_empty then
-                       v.warning(self, "Warning: superfluous parentheses.")
+                       v.warning(self, "parentheses", "Warning: superfluous parentheses.")
                end
        end
 end
@@ -94,7 +110,7 @@ end
 redef class AParExpr
        redef fun warn_parentheses(v)
        do
-               v.warning(self, "Warning: superfluous parentheses.")
+               v.warning(self, "parentheses", "Warning: superfluous parentheses.")
        end
 end
 
@@ -102,7 +118,7 @@ redef class AParExprs
        redef fun after_simple_misc(v)
        do
                if n_exprs.is_empty then
-                       v.warning(self, "Warning: superfluous parentheses.")
+                       v.warning(self, "parentheses", "Warning: superfluous parentheses.")
                end
        end
 end
@@ -117,7 +133,7 @@ redef class AReturnExpr
        end
 end
 
-redef class AContinueExpr
+redef class AEscapeExpr
        redef fun after_simple_misc(v)
        do
                var e = n_expr
@@ -127,34 +143,53 @@ redef class AContinueExpr
        end
 end
 
-redef class ABreakExpr
+redef class AWhileExpr
        redef fun after_simple_misc(v)
        do
-               var e = n_expr
-               if e != null then
-                       e.warn_parentheses(v)
+               if n_expr isa ATrueExpr then
+                       v.warning(self, "loop", "Warning: use `loop` instead of `while true do`.")
+               else
+                       n_expr.warn_parentheses(v)
                end
+               v.check_do_expr(n_block)
        end
 end
 
-redef class AWhileExpr
+redef class ADoExpr
        redef fun after_simple_misc(v)
        do
-               if n_expr isa ATrueExpr then
-                       v.warning(self, "Warning: use 'loop' instead of 'while true do'.")
-               else
-                       n_expr.warn_parentheses(v)
-               end
+               v.check_do_expr(n_block)
+       end
+end
+
+redef class ALoopExpr
+       redef fun after_simple_misc(v)
+       do
+               v.check_do_expr(n_block)
        end
 end
 
 redef class AForExpr
        redef fun after_simple_misc(v)
        do
+               v.check_do_expr(n_block)
+       end
+end
+
+redef class AForGroup
+       redef fun after_simple_misc(v)
+       do
                n_expr.warn_parentheses(v)
        end
 end
 
+redef class AWithExpr
+       redef fun after_simple_misc(v)
+       do
+               v.check_do_expr(n_block)
+       end
+end
+
 redef class AIfExpr
        redef fun after_simple_misc(v)
        do
@@ -173,7 +208,7 @@ redef class AOnceExpr
        redef fun accept_simple_misc(v)
        do
                if v.once_count > 0 then
-                       v.warning(self, "Useless once in a once expression.")
+                       v.warning(self, "nested-once", "Warning: useless once in a once expression.")
                end
                v.once_count = v.once_count + 1
 
@@ -182,3 +217,25 @@ redef class AOnceExpr
                v.once_count = v.once_count - 1
        end
 end
+
+redef class TSemi
+       redef fun accept_simple_misc_token(v)
+       do
+               var n = next_token
+               var p = prev_token
+               if
+                       n == null or
+                       n isa TEol or
+                       n isa EOF or
+                       n isa TComment or
+                       p == null or
+                       p isa TEol or
+                       p isa EOF or
+                       p isa TComment or
+                       p isa TSemi
+               then
+                       v.warning(self, "semi", "Warning: superfluous `;`.")
+                       return
+               end
+       end
+end