Skip to content

[MERGED] Improvements for detection of unused assignments #480

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

Closed
wants to merge 12 commits into from

Conversation

Daniel-Cortez
Copy link
Contributor

@Daniel-Cortez Daniel-Cortez commented Feb 2, 2020

What this PR does / why we need it:

This PR does the following:

  • Makes the compiler detect unused assignments that happen after the point where the variable is used:
main()
{
    new n = 1;
    printf("%d", n);
    n = 2; // warning 204: symbol is assigned a value that is never used: "n"
}
  • Adds warning 240 (previously assigned value is never used).
new x = 1;
x = 2; // warning 240: previously assigned value is never used (symbol "x")

Which issue(s) this PR fixes:

Fixes #479

What kind of pull this is:

  • A Bug Fix
  • A New Feature
  • Some repository meta (documentation, etc)
  • Other

Additional Documentation:

Currently warning 240 doesn't work on arrays, as it would require a lot more modifications to track assignments to each array element individually.
It is also worth noting that some of the code parts in this PR were copied from the implementation of warning 210, as both of them work by the same principle.

@Daniel-Cortez Daniel-Cortez requested a review from a team as a code owner February 2, 2020 18:11
@Y-Less
Copy link
Member

Y-Less commented Feb 13, 2020

I've not read the code fully. Does this handle operator=? There should probably be no warning in that case, since you are technically calling a function, which may have desirable side-effects.

@Daniel-Cortez
Copy link
Contributor Author

Not sure what you mean exactly. Can you give an example so I could check it (and see what I can do about it, if necessary)?

@Y-Less
Copy link
Member

Y-Less commented Feb 13, 2020

Tag:operator=(Tag:a)
{
    printf("hi");
    return Tag:(_:a * 2);
}

main()
{
    new Tag:a = 5, Tag:b;
    // Unused assignment that does something.
    b = a;
}

@Daniel-Cortez
Copy link
Contributor Author

Daniel-Cortez commented Feb 14, 2020

Done, overloaded assignments are now handled without printing warning 204.

@Daniel-Cortez Daniel-Cortez force-pushed the w240 branch 2 times, most recently from c1e49a5 to 456e837 Compare April 9, 2020 14:39
@Daniel-Cortez
Copy link
Contributor Author

Daniel-Cortez commented Apr 9, 2020

  • Rebased to resolve merge conflicts.
  • Improved detection of unused assignments for function arguments:
DoSomething(arg) // Now 'arg' is marked as `uASSIGNED` ("assigned a value
{                // that is not used yet") upon the beginning of function body.
    arg = 1; // warning 240: previously assigned value is never used
             // (previously the compiler couldn't detect this).
    #pragma unused arg
}

This allows to detect unused assignments inside "if" statements, e.g.
```
main()
{
    new var = 1;
    if (var)
        var = 0;
    // the value assigned to "var" is not used upon return
}
```
@Y-Less Y-Less closed this May 30, 2020
@Daniel-Cortez Daniel-Cortez changed the title Improvements for detection of unused assignments [MERGED] Improvements for detection of unused assignments Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants