From 6cb105dc6a70d5c0fe5c0f60b85b848d79abccea Mon Sep 17 00:00:00 2001 From: Max Doerner Date: Mon, 12 Oct 2020 01:29:19 +0200 Subject: [PATCH] Do not issue neg line number results for On Error GoTo -1 unless there is a line number -1 This PR also fixes a declaration resolver bug that removed the minus sign from negative line labels. --- .../NegativeLineNumberInspection.cs | 26 ++++++++++++++- .../DeclarationSymbolsListener.cs | 4 +-- .../ThunderCode/ThunderCodeInspectionTests.cs | 33 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Rubberduck.CodeAnalysis/Inspections/Concrete/ThunderCode/NegativeLineNumberInspection.cs b/Rubberduck.CodeAnalysis/Inspections/Concrete/ThunderCode/NegativeLineNumberInspection.cs index d0f7eac9a8..76c391197a 100644 --- a/Rubberduck.CodeAnalysis/Inspections/Concrete/ThunderCode/NegativeLineNumberInspection.cs +++ b/Rubberduck.CodeAnalysis/Inspections/Concrete/ThunderCode/NegativeLineNumberInspection.cs @@ -1,9 +1,12 @@ -using Antlr4.Runtime; +using System.Linq; +using Antlr4.Runtime; using Antlr4.Runtime.Tree; using Rubberduck.CodeAnalysis.Inspections.Abstract; using Rubberduck.Parsing; using Rubberduck.Parsing.Grammar; +using Rubberduck.Parsing.Symbols; using Rubberduck.Parsing.VBA; +using Rubberduck.Parsing.VBA.DeclarationCaching; using Rubberduck.Resources.Inspections; namespace Rubberduck.CodeAnalysis.Inspections.Concrete.ThunderCode @@ -31,6 +34,27 @@ protected override string ResultDescription(QualifiedContext return InspectionResults.NegativeLineNumberInspection.ThunderCodeFormat(); } + protected override bool IsResultContext(QualifiedContext context, DeclarationFinder finder) + { + return !IsOnErrorGotoMinusOne(context.Context) + || ProcedureHasMinusOneLabel(finder, context); + } + + private static bool IsOnErrorGotoMinusOne(ParserRuleContext context) + { + return context is VBAParser.OnErrorStmtContext onErrorStatement + && "-1".Equals(onErrorStatement.expression()?.GetText().Trim()); + } + + private static bool ProcedureHasMinusOneLabel(DeclarationFinder finder, QualifiedContext context) + { + return finder.Members(context.ModuleName, DeclarationType.LineLabel) + .Any(label => label.IdentifierName.Equals("-1") + && (label.ParentScopeDeclaration + .Context?.GetSelection() + .Contains(context.Context.GetSelection()) ?? false)); + } + private class NegativeLineNumberKeywordsListener : InspectionListenerBase { public override void EnterOnErrorStmt(VBAParser.OnErrorStmtContext context) diff --git a/Rubberduck.Parsing/VBA/DeclarationResolving/DeclarationSymbolsListener.cs b/Rubberduck.Parsing/VBA/DeclarationResolving/DeclarationSymbolsListener.cs index 02c4c87328..6e9d19ffe6 100644 --- a/Rubberduck.Parsing/VBA/DeclarationResolving/DeclarationSymbolsListener.cs +++ b/Rubberduck.Parsing/VBA/DeclarationResolving/DeclarationSymbolsListener.cs @@ -646,8 +646,8 @@ private void AddIdentifierStatementLabelDeclaration(VBAParser.IdentifierStatemen private void AddLineNumberLabelDeclaration(VBAParser.LineNumberLabelContext context) { - var statementText = context.numberLiteral().GetText(); - var statementSelection = context.numberLiteral().GetSelection(); + var statementText = context.GetText().Trim(); + var statementSelection = context.GetSelection(); AddDeclaration( CreateDeclaration( diff --git a/RubberduckTests/Inspections/ThunderCode/ThunderCodeInspectionTests.cs b/RubberduckTests/Inspections/ThunderCode/ThunderCodeInspectionTests.cs index 2c0c910f40..3fdc28b62f 100644 --- a/RubberduckTests/Inspections/ThunderCode/ThunderCodeInspectionTests.cs +++ b/RubberduckTests/Inspections/ThunderCode/ThunderCodeInspectionTests.cs @@ -224,6 +224,39 @@ GoTo 1 GoTo -5 1 -5: +End Sub")] + [TestCase(1, @"Public Sub Gogo() +On Error GoTo 1 +1 +-1: +End Sub")] + [TestCase(2, @"Public Sub Gogo() +On Error GoTo -1 +1 +-1: +End Sub")] + [TestCase(2, @"Public Sub Gogo() +On Error GoTo -1 +1: +-1 +End Sub")] + [TestCase(0, @"Public Sub Gogo() +On Error GoTo -1 +1 +End Sub")] + [TestCase(1, @"Public Sub Gogo() +On Error GoTo -2 +1 +End Sub")] + [TestCase(2, @"Public Sub Gogo() +On Error GoTo -5 +1 +-5: +End Sub")] + [TestCase(2, @"Public Sub Gogo() +On Error GoTo -5 +1: +-5 End Sub")] public void NegativeLineNumberLabel_ReturnResults(int expectedCount, string inputCode) {