TabularEditor / TabularEditor3

Bug reports, feature requests, discussion and documentation for Tabular Editor 3 (commercial version).
61 stars 7 forks source link

Unused variable doesn't report all of them #1186

Closed edhans closed 5 months ago

edhans commented 5 months ago

Description

This might be by design, but not sure. Here is my measure. As you can tell there is a bunch of code I abandoned and will now comment out so this new feature doesn't report it, but it is only telling me the Result variable is unused, when in fact all of them are unused on lines 21-26.

image

Tabular Editor 3 Version

3.12.1

Screenshots

No response

Steps to Reproduce

No response

Expected behavior

No response

Crash Report

No response

Windows Version

windows 11

otykier commented 5 months ago

Hi Ed, The current behavior is actually by design, but we're certainly open for discussion on this. We treat a variable as being unused only if there are no references to that variable anywhere in the code. If a variable is indirectly unused, such as in your example, then we still regard it as being in use - chances are that you're currently writing a DAX expression that you just haven't completely finished. On the other hand, if you are cleaning up some old code, we still show you that at least one variable is unused, and when you delete that, any leftover unused variables should then light up.

Btw. please note that we are changing this feature slightly in 3.13.0, so only the variable name and not the full expression gets highlighted, if the variable is unused (as per #1173).

image

edhans commented 5 months ago

Thanks. Makes sense. I like the new way to only highlight the variable name vs all of the code.

I'm ok with just showing variables that have no references. When cleaning up code, I'd need to work backwards to get them all, but without this, they would all show up as I was writing code, so no perfect answer here.

This is super useful. I've already found a bug in my code because of this - code I wrote that should have been referenced was not. Some yearly calcs were referencing a quarterly table variable. 😩

ricky7uio commented 4 months ago

@otykier Hi Daniel, I love the new warning. Is helping us a lot!

Is there a way to, programmatically with a C# script, identify all the measures with unused variables?

Thanks!!

otykier commented 4 months ago

This script works, but it uses the undocumented GetCachedSemantics() method, which is not available in TE2 and which may change in the future.

using Dax.Analyzer.Ast;

var output = "";
foreach (var measure in Model.AllMeasures)
{
    var messages = measure.GetCachedSemantics().Messages;
    if(messages.FirstOrDefault(m => m.Type == MessageType.Warning && m.Message.StartsWith("Variable") && m.Message.EndsWith("is declared but never used.")) is DocumentMessage message)
    {
        output += measure.DaxObjectFullName + ": " + message.Message + Environment.NewLine;
    }
}

Info(output);

image

ricky7uio commented 4 months ago

@otykier Thank you so much!