How to detect unused variable with analyzer package?

74 views
Skip to first unread message

Patrice Tisserand

unread,
Jan 27, 2023, 11:26:49 AM1/27/23
to Dart Analyzer Discussion
Hi,
I'm working on a dart code generator using code builder and I'm wondering how I can check in dart test if my generated source code contains any unused variables?

I have read API documentation but I didn't understand how I could achieve it, any hint?
Should I look at `dart analyze` source code to understand how I could mimic this behavior ?

Thanks a lot for your help.
Regards,
Patrice



Brian Wilkerson

unread,
Jan 27, 2023, 11:45:43 AM1/27/23
to analyzer...@dartlang.org
That depends on what kind of unused variables you're looking for. If we're talking about variables whose scope (visibility) is limited to known locations, then it's fairly easy.

https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/doc/tutorial/analysis.md should explain how to use an `AnalysisContextCollection` to get an `AnalysisSession`. Given an `AnalysisSession` you can use `getResolvedLibrary` to get the fully resolved ASTs for each of the libraries where the variable could be visible and for the library that declares the variable. First traverse the AST of the declaring library to find the declaration in question so that you can access the `declaredElement`, then traverse all of the ASTs (including the declaring library) to find `SimpleIdentifier`s whose static element is equal to the declared element. If you don't find any, then the variable is unreferenced.

(Depending on what you need, it might also be useful to check the `errors` associated with the individual `units` in the result. For local variables there should be a diagnostic indicating that they're unused. That won't cover all possible variables, though, so it isn't a replacement for the more general technique above.)

Let me know if you have additional questions.

--
You received this message because you are subscribed to the Google Groups "Dart Analyzer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to analyzer-discu...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/c557ff4a-e02c-434e-90f5-07f461c87c2fn%40dartlang.org.

Dmitry Zhifarsky

unread,
Jan 27, 2023, 11:55:17 AM1/27/23
to Dart Analyzer Discussion
You can take a look on how unused code search is implemented in DCM https://github.com/dart-code-checker/dart-code-metrics/tree/master/lib/src/analyzers/unused_code_analyzer.
This implementation searches for top level declarations, but tweaking it into what you need might be easier than writing from scratch.

If you have any questions, feel free to reach out.

пятница, 27 января 2023 г. в 20:45:43 UTC+4, brianwilkerson:

Patrice Tisserand

unread,
Jan 31, 2023, 5:08:14 AM1/31/23
to analyzer...@dartlang.org
Thanks a lot for your reply! 
By checking `errors` for my unit I can test my source code generator..

I take note of  the general technique for future usage.

Regards,
Patrice


Reply all
Reply to author
Forward
0 new messages