crystal-ameba / ameba

A static code analysis tool for Crystal
https://crystal-ameba.github.io
MIT License
514 stars 35 forks source link

Recognize unused variable from type declaration #429

Closed straight-shoota closed 8 months ago

straight-shoota commented 9 months ago

Lint/UselessAssign rule reports local variables that are defined per assignment but never used.

a = "" # Lint/UselessAssign: Useless assignment to variable `b`

Type declarations also declare a variable yet do not assign to the variable. This should even more reason to report it.

For the following program, ameba should report a similar issue:

a : String

This came up in https://github.com/crystal-lang/crystal/issues/14058

straight-shoota commented 8 months ago

@Sija citing https://github.com/crystal-ameba/ameba/pull/430#issuecomment-1854999845 here for clarity:

As type declarations are often used as part of DSLs and within accessor macros, I see no reliable way to mark them as unused assignments.

I'm not sure I get the issue you're seeing. For all I can tell, the use of type declarations in DSLs and accessor macros should be pretty much the same as with assignments. Ameba is able to handle getter foo = 1, right? Why is getter foo : Int32 problematic?

Sija commented 8 months ago

For all I can tell, the use of type declarations in DSLs and accessor macros should be pretty much the same as with assignments.

Ameba itself allows/uses them within the properties macro. Perhaps it would be possible to exclude 'em somehow... just thinking out loud here.

Ameba is able to handle getter foo = 1, right? Why is getter foo : Int32 problematic?

Good question. I believe it's due to some custom logic I've missed in the PR.