-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rubberduck-vba/next
Riportiamoci a pari
- Loading branch information
Showing
9 changed files
with
298 additions
and
7 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
Rubberduck.CodeAnalysis/Inspections/Concrete/UDTMemberNotUsedInspection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Rubberduck.CodeAnalysis.Inspections.Abstract; | ||
using Rubberduck.CodeAnalysis.Inspections.Extensions; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Parsing.VBA.DeclarationCaching; | ||
using Rubberduck.Resources.Inspections; | ||
using System.Linq; | ||
|
||
namespace Rubberduck.CodeAnalysis.Inspections.Concrete | ||
{ | ||
/// <summary> | ||
/// Warns about User Defined Type (UDT) members that are never referenced. | ||
/// </summary> | ||
/// <why> | ||
/// Declarations that are never used should be removed. | ||
/// </why> | ||
/// <example hasResult="true"> | ||
/// <module name="MyModule" type="Standard Module"> | ||
/// <![CDATA[ | ||
/// Private Type TTestModule | ||
/// FirstVal As Long | ||
/// End Type | ||
/// | ||
/// Private this As TTestModule | ||
/// ]]> | ||
/// </module> | ||
/// </example> | ||
/// <example hasResult="false"> | ||
/// <module name="MyModule" type="Standard Module"> | ||
/// <![CDATA[ | ||
/// Private Type TTestModule | ||
/// FirstVal As Long | ||
/// End Type | ||
/// | ||
/// Private this As TTestModule | ||
/// | ||
/// 'UDT Member is assigned but not used | ||
/// Public Sub DoSomething() | ||
/// this.FirstVal = 42 | ||
/// End Sub | ||
/// ]]> | ||
/// </module> | ||
/// </example> | ||
/// <example hasResult="false"> | ||
/// <module name="MyModule" type="Standard Module"> | ||
/// <![CDATA[ | ||
/// Private Type TTestModule | ||
/// FirstVal As Long | ||
/// End Type | ||
/// | ||
/// Private this As TTestModule | ||
/// | ||
/// 'UDT Member is assigned and read | ||
/// Public Sub DoSomething() | ||
/// this.FirstVal = 42 | ||
/// Debug.Print this.FirstVal | ||
/// End Sub | ||
/// ]]> | ||
/// </module> | ||
/// </example> | ||
internal sealed class UDTMemberNotUsedInspection : DeclarationInspectionBase | ||
{ | ||
public UDTMemberNotUsedInspection(IDeclarationFinderProvider declarationFinderProvider) | ||
: base(declarationFinderProvider, DeclarationType.UserDefinedTypeMember) | ||
{} | ||
|
||
protected override bool IsResultDeclaration(Declaration declaration, DeclarationFinder finder) | ||
{ | ||
return declaration.DeclarationType.Equals(DeclarationType.UserDefinedTypeMember) | ||
&& !declaration.References.Any(); | ||
} | ||
|
||
protected override string ResultDescription(Declaration declaration) | ||
{ | ||
var declarationType = declaration.DeclarationType.ToLocalizedString(); | ||
var declarationName = declaration.IdentifierName; | ||
return string.Format( | ||
InspectionResults.IdentifierNotUsedInspection, | ||
declarationType, | ||
declarationName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 18 additions & 6 deletions
24
Rubberduck.Resources/Inspections/InspectionInfo.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Rubberduck.Resources/Inspections/InspectionResults.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
RubberduckTests/Inspections/UDTMemberNotUsedInspectionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
using NUnit.Framework; | ||
using Rubberduck.CodeAnalysis.Inspections; | ||
using Rubberduck.CodeAnalysis.Inspections.Concrete; | ||
using Rubberduck.Parsing.VBA; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace RubberduckTests.Inspections | ||
{ | ||
[TestFixture] | ||
class UDTMemberNotUsedInspectionTests : InspectionTestsBase | ||
{ | ||
[Test] | ||
[Category("Inspections")] | ||
[Category(nameof(UDTMemberNotUsedInspection))] | ||
public void ReturnsZeroResult() | ||
{ | ||
const string inputCode = | ||
@" | ||
Option Explicit | ||
Private Type TUnitTest | ||
FirstVal As Long | ||
SecondVal As Long | ||
End Type | ||
Private this As TUnitTest | ||
Private Sub TestSub(testVal As Long) | ||
this.FirstVal = testVal * 2 | ||
this.SecondVal = testVal | ||
End Sub | ||
"; | ||
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count()); | ||
} | ||
|
||
[Test] | ||
[Category("Inspections")] | ||
[Category(nameof(UDTMemberNotUsedInspection))] | ||
public void ReturnsSingleResult() | ||
{ | ||
const string inputCode = | ||
@" | ||
Option Explicit | ||
Private Type TUnitTest | ||
FirstVal As Long | ||
SecondVal As Long | ||
End Type | ||
Private this As TUnitTest | ||
Private Sub TestSub(testVal As Long) | ||
this.FirstVal = testVal | ||
End Sub | ||
"; | ||
Assert.AreEqual(1, InspectionResultsForStandardModule(inputCode).Count()); | ||
} | ||
|
||
[Test] | ||
[Category("Inspections")] | ||
[Category(nameof(UDTMemberNotUsedInspection))] | ||
public void ReturnsManyResults() | ||
{ | ||
const string inputCode = | ||
@" | ||
Option Explicit | ||
Private Type TUnitTest | ||
FirstVal As Long | ||
SecondVal As Long | ||
ThirdVal As Long | ||
End Type | ||
Private this As TUnitTest | ||
Private Sub TestSub(testVal As Long) | ||
this.SecondVal = testVal | ||
End Sub | ||
"; | ||
Assert.AreEqual(2, InspectionResultsForStandardModule(inputCode).Count()); | ||
} | ||
|
||
[Test] | ||
[Category("Inspections")] | ||
[Category(nameof(UDTMemberNotUsedInspection))] | ||
public void ReturnsResultForNestedUDTMember() | ||
{ | ||
const string inputCode = | ||
@" | ||
Option Explicit | ||
Private Type TPair | ||
IDNumber As Long | ||
IDName As String | ||
End Type | ||
Private Type TUnitTest | ||
ID_Name_Pair As TPair | ||
SecondVal As Long | ||
End Type | ||
Private this As TUnitTest | ||
Private Sub TestSub(testVal As Long) | ||
this.ID_Name_Pair.IDNumber = testVal | ||
this.SecondVal = testVal * 2 | ||
End Sub | ||
"; | ||
Assert.AreEqual(1, InspectionResultsForStandardModule(inputCode).Count()); | ||
} | ||
|
||
[Test] | ||
[Category("Inspections")] | ||
[Category(nameof(UDTMemberNotUsedInspection))] | ||
public void RespectsIgnoreAnnotation() | ||
{ | ||
const string inputCode = | ||
@" | ||
Option Explicit | ||
Private Type TUnitTest | ||
FirstVal As Long | ||
'@Ignore UDTMemberNotUsed | ||
SecondVal As Long | ||
End Type | ||
Private this As TUnitTest | ||
Private Sub TestSub(testVal As Long) | ||
this.FirstVal = testVal | ||
End Sub | ||
"; | ||
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count()); | ||
} | ||
|
||
[TestCase("'@IgnoreModule")] | ||
[TestCase("'@IgnoreModule UDTMemberNotUsed")] | ||
[Category("Inspections")] | ||
[Category(nameof(UDTMemberNotUsedInspection))] | ||
public void RespectsIgnoreModuleAnnotation(string annotation) | ||
{ | ||
var inputCode = | ||
$@" | ||
{annotation} | ||
Option Explicit | ||
Private Type TUnitTest | ||
FirstVal As Long | ||
SecondVal As Long | ||
End Type | ||
Private this As TUnitTest | ||
Private Sub TestSub(testVal As Long) | ||
this.FirstVal = testVal | ||
End Sub | ||
"; | ||
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count()); | ||
} | ||
|
||
protected override IInspection InspectionUnderTest(RubberduckParserState state) | ||
{ | ||
return new UDTMemberNotUsedInspection(state); | ||
} | ||
} | ||
} |