crystal-ameba / ameba

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

Custom macro generates "Useless assignment to variable" #456

Closed petr-fischer closed 5 months ago

petr-fischer commented 5 months ago

Example correct code:

class AAA 
  macro reactive_property(declaration)
  end 

  property placeholder_key : String?
  reactive_property placeholder_key2 : String?
  reactive_property in_edit = false                                                                                                                             
end

Then:

docker pull ghcr.io/crystal-ameba/ameba
docker run --rm -v "$(pwd):/src" ghcr.io/crystal-ameba/ameba

Wrong output:

test.cr:6:21
[W] Lint/UselessAssign: Useless assignment to variable `placeholder_key2`
> reactive_property placeholder_key2 : String?
                    ^------------------------^

test.cr:7:21
[W] Lint/UselessAssign: Useless assignment to variable `in_edit`
> reactive_property in_edit = false
                    ^-----^

Related: #77

veelenga commented 5 months ago

I think this was explicitly noted here https://github.com/crystal-ameba/ameba/pull/430#issuecomment-1871646886 that such cases can't (or very hard) to be properly handled. There is a configuration property exclude_type_declarations which excludes the type declarations from the check, however it will not help here with a second example (in_edit property).

@Sija would you like to hop-in? Should we add another property to handle that?

Sija commented 5 months ago

@veelenga That is correct. There's not much we can do here.

petr-fischer commented 5 months ago

OK, so I'm sticking with the ignore rules in the config. Thanks for the recheck and explanation.