Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid define is unused warning #155

Open
tothlac opened this issue Jul 28, 2021 · 17 comments
Open

Invalid define is unused warning #155

tothlac opened this issue Jul 28, 2021 · 17 comments
Labels
bug Something isn't working

Comments

@tothlac
Copy link

tothlac commented Jul 28, 2021

Bug Description

It's a very nice plugin, so probably we would like to use it on our repositories.
Unfortunately, it looks like sometimes there are false positive warnings.

To Reproduce

Run rebar3 hank on this simple code

-module(myapp).

-define(EXCEPTION(C, R, Stacktrace), C:R:Stacktrace).

-export([f/0]).

-define(RESULT, 1).

-spec f() -> term().
f() ->
    try
        ?RESULT
    catch ?EXCEPTION(_, _Reason, _St) ->
              ok
    end.

Expected Behavior

hank returns the following false positive:

(base) tothlac:myapp rebar3 hank
===> Looking for code to kill with fire...
===> The following pieces of code are dead and should be removed:
src/myapp.erl:7: ?RESULT is unused

, and it should not complain.

@tothlac tothlac added the bug Something isn't working label Jul 28, 2021
@elbrujohalcon
Copy link
Collaborator

elbrujohalcon commented Jul 28, 2021

Oh, man! Can I hate macros even more??

I didn't check but most likely hank can't parse your function and that's why it doesn't detect macro usage.

I'm sorry but fixing this will not be easy. You'll be better off adding a -hank attribute to your module to ignore the warning.

@pbrudnick
Copy link
Collaborator

This Node is difficult to handle:

text - {tree,text,
             {attr,10,[],none},
             "f( ) -> try ?RESULT catch ?EXCEPTION( _ , _Reason , _St ) -> ok end .\n"}

@tothlac
Copy link
Author

tothlac commented Jul 28, 2021

Hi Brujo. The plugin is really nice, it will be definitely quite useful for our repositories.
Today I was able to find a huge amount of dead code (not used parameters, not used defines) in our code.

The problem is that we have 100 repositories. In these 100 repositories, there are at least 2 thousand modules, and I'm afraid this fake warning occurs at least in 100 modules because we use macros like EXCEPTION in a lot of places. Actually, hank works fine without the EXCEPTION macro, so this module is ok:

-module(myapp).

-define(EXCEPTION(C, R, Stacktrace), C:R:Stacktrace).

-export([f/0]).

-define(RESULT, 1).

-spec f() -> term().
f() ->
    try
        ?RESULT
    catch C:R:Stacktrace ->
              ok
    end.

So most likely I will delete the EXCEPTION macro from everywhere (We used it when we were switching from OTP 21 to 22).

Adding it to the list of ignored modules would not be really good because that way we would hide lots of other warnings in those modules.

I'm going to check what warnings it will report without the EXCEPTION and STACKTRACE macros. Hopefully, there won't be any kind of false positives after this modification.

@paulo-ferraz-oliveira
Copy link
Contributor

It's also marginally linked to erlang/otp#4479. Once Erlang/OTP can detect same-module unused macros, part of your problem will go away, unless you're using a shared .hrl, and then it's a different class of issues altogether, as Brujo points out in the linked issue.

@elbrujohalcon
Copy link
Collaborator

Adding it to the list of ignored modules would not be really good because that way we would hide lots of other warnings in those modules.

FWIW... You don't need to ignore the whole module. Hank allows you to ignore specific warnings. Check the Readme for the appropriate syntax.

@tothlac
Copy link
Author

tothlac commented Jul 29, 2021

I've seen that, just like in elvis you can add a file to the ignore list of some specific rules. That's fine, but it wold be still probably 50-100 modules in all of our repositories, so I don't think ignoring those modules would be the best option.

@tothlac
Copy link
Author

tothlac commented Jul 29, 2021

Anyways, as you have said fixing this issue would take quite some time, the ticket should be closed.

@elbrujohalcon
Copy link
Collaborator

elbrujohalcon commented Jul 29, 2021

I've seen that, just like in elvis you can add a file to the ignore list of some specific rules. That's fine, but it wold be still probably 50-100 modules in all of our repositories, so I don't think ignoring those modules would be the best option.

But also, somewhat similarly to Elvis as well, you can add a specific flag to your modules to ignore only the warning that you want to ignore but keep analyzing the rest of the file. Something like…

-hank([{unused_macros, ["RESULT"]}]).

…or more specifically…

-hank([{unused_macros, [{"RESULT", none}]}]).

@elbrujohalcon
Copy link
Collaborator

elbrujohalcon commented Jul 29, 2021

Anyways, as you have said fixing this issue would take quite some time, the ticket should be closed.

I'll keep it open, but I'll move it to the Eventually 🙄 milestone 😉

@elbrujohalcon elbrujohalcon added this to the Eventually 🙄 milestone Jul 29, 2021
@paulo-ferraz-oliveira
Copy link
Contributor

I fiddled around with erl_syntax, erl_scan and erl_parse. Do you think the answer will go through these?

@elbrujohalcon
Copy link
Collaborator

Most likely… But also ktn_dodger… or epp_dodger… or… maybe… eventually… the new parser from erlfmt is introduced into OTP… it becomes the official parser and then we switch to it.

@tothlac
Copy link
Author

tothlac commented Aug 2, 2021

Hi. Maybe it is another error but looks like there are other special cases when hank is not able to parse everything correctly. For example when you use string concatenation without ++ operator, like in the example below:

-module(hank_problem).

-export([world/0]).

-define(WORLD, "world").

world() ->
    "hello " ?WORLD.

WORLD macro is used. hank reports this:

===> The following pieces of code are dead and should be removed:
src/hank_problem.erl:5: ?WORLD is unused

@elbrujohalcon
Copy link
Collaborator

Same thing. String concat with macros is unparseable.

@tothlac
Copy link
Author

tothlac commented Aug 2, 2021

Thanks. That's why I created it as part of this ticket.

@tothlac
Copy link
Author

tothlac commented Aug 2, 2021

Well, probably the easiest workaround here is to use the ++ opeator.

@tothlac
Copy link
Author

tothlac commented Aug 2, 2021

I've tried and fortunately using that syntax hank does not complain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants