File tree Expand file tree Collapse file tree 4 files changed +28
-12
lines changed
Rubberduck.CodeAnalysis/Inspections/Concrete
Rubberduck.Parsing/Grammar Expand file tree Collapse file tree 4 files changed +28
-12
lines changed Original file line number Diff line number Diff line change 55using Rubberduck . VBEditor ;
66using System . Collections . Generic ;
77using System . Diagnostics ;
8+ using Antlr4 . Runtime . Tree ;
89
910namespace Rubberduck . Inspections . Concrete
1011{
@@ -35,12 +36,12 @@ public void AddResult(QualifiedContext<ParserRuleContext> qualifiedContext)
3536
3637 private bool BlockContainsExecutableStatements ( VBAParser . BlockContext block )
3738 {
38- return block ? . children != null && ContainsExecutableStatements ( block ) ;
39+ return block ? . children != null && ContainsExecutableStatements ( block . children ) ;
3940 }
4041
41- private bool ContainsExecutableStatements ( VBAParser . BlockContext block )
42+ private bool ContainsExecutableStatements ( IList < IParseTree > blockChildren )
4243 {
43- foreach ( var child in block . children )
44+ foreach ( var child in blockChildren )
4445 {
4546 if ( child is VBAParser . BlockStmtContext blockStmt )
4647 {
@@ -76,5 +77,18 @@ child is VBAParser.CommentOrAnnotationContext ||
7677
7778 return false ;
7879 }
80+
81+ public void InspectBlockForExecutableStatements < T > ( VBAParser . UnterminatedBlockContext block , T context ) where T : ParserRuleContext
82+ {
83+ if ( ! BlockContainsExecutableStatements ( block ) )
84+ {
85+ AddResult ( new QualifiedContext < ParserRuleContext > ( CurrentModuleName , context ) ) ;
86+ }
87+ }
88+
89+ private bool BlockContainsExecutableStatements ( VBAParser . UnterminatedBlockContext block )
90+ {
91+ return block ? . children != null && ContainsExecutableStatements ( block . children ) ;
92+ }
7993 }
8094}
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public class EmptyForEachBlockListener : EmptyBlockInspectionListenerBase
3333 {
3434 public override void EnterForEachStmt ( [ NotNull ] VBAParser . ForEachStmtContext context )
3535 {
36- InspectBlockForExecutableStatements ( context . block ( ) , context ) ;
36+ InspectBlockForExecutableStatements ( context . unterminatedBlock ( ) , context ) ;
3737 }
3838 }
3939 }
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public class EmptyForloopBlockListener : EmptyBlockInspectionListenerBase
3333 {
3434 public override void EnterForNextStmt ( [ NotNull ] VBAParser . ForNextStmtContext context )
3535 {
36- InspectBlockForExecutableStatements ( context . block ( ) , context ) ;
36+ InspectBlockForExecutableStatements ( context . unterminatedBlock ( ) , context ) ;
3737 }
3838 }
3939 }
Original file line number Diff line number Diff line change @@ -108,7 +108,7 @@ moduleBodyElement :
108108
109109block : (blockStmt endOfStatement)*;
110110
111- unterminatedBlock : blockStmt? (endOfStatement blockStmt)*;
111+ unterminatedBlock : blockStmt (endOfStatement blockStmt)*;
112112
113113blockStmt :
114114 statementLabelDefinition whiteSpace? mainBlockStmt?
@@ -353,16 +353,18 @@ eventStmt : (visibility whiteSpace)? EVENT whiteSpace identifier whiteSpace? arg
353353exitStmt : EXIT_DO | EXIT_FOR | EXIT_FUNCTION | EXIT_PROPERTY | EXIT_SUB ;
354354
355355forEachStmt :
356- FOR whiteSpace EACH whiteSpace expression whiteSpace IN whiteSpace expression endOfStatement
357- block
358- statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
356+ FOR whiteSpace EACH whiteSpace expression whiteSpace IN whiteSpace expression
357+ (endOfStatement unterminatedBlock)?
358+ (endOfStatement statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
359+ | whiteSpace? COMMA whiteSpace? expression)
359360;
360361
361362// expression EQ expression refactored to expression to allow SLL
362363forNextStmt :
363- FOR whiteSpace expression whiteSpace TO whiteSpace expression stepStmt? whiteSpace* endOfStatement
364- block
365- statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
364+ FOR whiteSpace expression whiteSpace TO whiteSpace expression stepStmt? whiteSpace*
365+ (endOfStatement unterminatedBlock)?
366+ (endOfStatement statementLabelDefinition? whiteSpace? NEXT (whiteSpace expression)?
367+ | whiteSpace? COMMA whiteSpace? expression)
366368;
367369
368370stepStmt : whiteSpace STEP whiteSpace expression;
You can’t perform that action at this time.
0 commit comments