Rubberduck: Code Inspection erroneously identifies Assignment not in use

Created on 19 Jul 2019  路  4Comments  路  Source: rubberduck-vba/Rubberduck

Version 2.4.1.4627
OS: Microsoft Windows NT 6.2.9200.0, x64
Host Product: Microsoft Office 2010 x86
Host Version: 14.0.7232.5000
Host Executable: EXCEL.EXE

Description
When running code review, certain variables get marked as 'Assignment not in use' when this is not true.

To Reproduce

  1. Create a Function with code below
  2. Run Code Inspection
    dim Table as range
    dim HEADER as long
    Header = 3
    ' Grab Full range of data including top of sheet
    Set Table = thisworkbook.worksheet("Sheet 1").Cells(3, 1).CurrentRegion
    ' Remove the top of the sheet and only select data and header row
    Set Table = Table.Offset(HEADER - 1).Resize(Table .Rows.Count - (HEADER - 1))

Expected behavior
The code above sets the range to a specific place and then uses that to offset it's location to get a more precise subset of data. The Table variable gets marked as Assignment unused, when in fact it is being used in the very next line in order to get better precision.

Additional context
I feel as though the code inspection is trying to see if a variable gets assigned and then if the variable gets assigned right away before being used, but it should also look that if it is being assigned again whether it is also part of the assignment.

Makes sense to mark this as assignment unused
X = A
X = B

Doesn't make sense to mark this as assignment unused
X = A
X = X.Offset(B)

bug code-path-analysis duplicate feature-inspections inspection-false-positive

Most helpful comment

Thanks for reporting this.

This seems to essentially be a duplicate of #4913.

All 4 comments

Repro confirmed. The good news is that the resolver correctly picks up every single part of the RHS expression in the provided example, so this shouldn't be too hard to fix:

https://github.com/rubberduck-vba/Rubberduck/blob/f5e9c2afa3894b3b0d8594dfd1f64cd74c5af817/Rubberduck.CodeAnalysis/Inspections/Concrete/AssignmentNotUsedInspection.cs#L66-L70

I think all we need to do is to rewrite that LINQ query so that we keep r.Context.Parent is VBAParser.SetStmtContext setStmtContext around for further querying, and discard the IdentifierReference when the RHS of setStmtContext contains one or more identifier references for the variable.

Thanks for reporting this.

This seems to essentially be a duplicate of #4913.

hmm, it's not just SetStmtContext, we need to also check for LetStmtContext. Indeed a duplicate.

Sorry for the duplication; thank you for the quick turnaround!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

retailcoder picture retailcoder  路  3Comments

DecimalTurn picture DecimalTurn  路  3Comments

ghost picture ghost  路  3Comments

retailcoder picture retailcoder  路  3Comments

philippetev picture philippetev  路  3Comments