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

Re-add dead code detection #21096

Merged
merged 1 commit into from
Feb 15, 2021
Merged

Re-add dead code detection #21096

merged 1 commit into from
Feb 15, 2021

Conversation

flack
Copy link
Contributor

@flack flack commented Feb 6, 2021

This re-adds unreachable code detection for Python based on vulture.

Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the --min-confidence 100 setting. From https://pypi.org/project/vulture/:

Use --min-confidence 100 to only report code that is guaranteed to be unused within the analyzed files.

So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a return statement). As such, there is not suppressions list.

My motivation was mainly #21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because #21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

@flack
Copy link
Contributor Author

flack commented Feb 6, 2021

meh, turns out clicked the wrong button, and now I can't mark it as Draft anymore... modified the title instead

@flack flack changed the title Re-add dead code detection [draft] Re-add dead code detection Feb 6, 2021
@maflcko maflcko marked this pull request as draft February 6, 2021 20:15
@DrahtBot DrahtBot added the Tests label Feb 6, 2021
@fanquake fanquake changed the title [draft] Re-add dead code detection Re-add dead code detection Feb 7, 2021
@flack
Copy link
Contributor Author

flack commented Feb 8, 2021

#21081 has been merged and the other issue I mentioned has been addressed in #21107, so this should now be ready for review. I also bumped the vulture dependency to 2.3 since it turns out Python 3.5 support is no longer needed

@flack flack marked this pull request as ready for review February 8, 2021 18:53
@maflcko
Copy link
Member

maflcko commented Feb 8, 2021

Please squash your commits according to https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#squashing-commits

@flack
Copy link
Contributor Author

flack commented Feb 8, 2021

@MarcoFalke done

@fanquake
Copy link
Member

fanquake commented Feb 9, 2021

Seems like at --min-confidence=100 vulture will just report unreachable code.
You have to drop it to 90 for it to report unused imports:

diff --git a/test/functional/test_framework/key.py b/test/functional/test_framework/key.py
index e0cbab45c..d0f3472e9 100644
--- a/test/functional/test_framework/key.py
+++ b/test/functional/test_framework/key.py
@@ -14,6 +14,10 @@ import unittest
 
 from .util import modinv
 
+import nonsense
+
 def TaggedHash(tag, data):
     ss = hashlib.sha256(tag.encode('utf-8')).digest()
     ss += ss
bitcoin/test/functional/test_framework/key.py:17: unused import 'nonsense' (90% confidence)

or 60 before it will report unused functions, i.e xor_bytes in #21100:

bitcoin/test/functional/test_framework/key.py:23: unused function 'xor_bytes' (60% confidence)

Looking at the vulture source this makes sense, because the default confidence for basically all checks is 60.

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Feb 9, 2021
c9095b7 test: remove unnecessary assignment in bdb (Bruno Garcia)

Pull request description:

  This PR removes the unnecessary assignment to page_info['entries'] on line 54 since there is another assignment for it in line 59.

  I think a lint (bitcoin#21096) would detect cases like this one.

ACKs for top commit:
  achow101:
    ACK c9095b7
  theStack:
    Code Review ACK c9095b7

Tree-SHA512: 23377077c015b04361fd416b41bf6806ad0bdd4d264be6760f0fd3bc88d694d2cd52cae250519925c5d3b3c70715772714c3863f8fa181a2eb4883204ccdbf9d
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 9, 2021
c9095b7 test: remove unnecessary assignment in bdb (Bruno Garcia)

Pull request description:

  This PR removes the unnecessary assignment to page_info['entries'] on line 54 since there is another assignment for it in line 59.

  I think a lint (bitcoin#21096) would detect cases like this one.

ACKs for top commit:
  achow101:
    ACK c9095b7
  theStack:
    Code Review ACK c9095b7

Tree-SHA512: 23377077c015b04361fd416b41bf6806ad0bdd4d264be6760f0fd3bc88d694d2cd52cae250519925c5d3b3c70715772714c3863f8fa181a2eb4883204ccdbf9d
@laanwj
Copy link
Member

laanwj commented Feb 10, 2021

NACK on this. I still stand by discussion in #16961. Even having to fine-tune the threshold value just seems a recipe for endless frustration.

@flack
Copy link
Contributor Author

flack commented Feb 10, 2021

@laanwj I'm not going to spend too much time arguing, but the rationale for #16961 was to avoid false positives. --min-confidence=100 accomplished that, since it is guaranteed to only find dead code (inside of if (false), after return statement or similar). So there isn't any need to fine-tune anything. This is just a little sanity check that even on my five year old laptop completes in less than one second, and it would have caught an instance of xkcd 221 in the taproot tests, which is probably one of the better-reviewed PRs. What's not to like?

@practicalswift
Copy link
Contributor

Concept ACK

If simply specifying --min-confidence 100 means that the false positives we've seen in the past are avoided then this should be an obvious win, no? :)

@laanwj, is your NACK made assuming that --min-confidence 100 is not sufficient to solve the false positive issues we've seen in the past? Not questioning: just trying to understand the reasoning :)

@laanwj
Copy link
Member

laanwj commented Feb 11, 2021

Okay retracted the NACK if everyone else wants this it's okay with me I just would really prefer if it doesn't come up as issue again.

# Any value below 100 introduces the risk of false positives, which would create an unacceptable maintenance burden.
if ! vulture \
--min-confidence 100 \
$(git rev-parse --show-toplevel); then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be $(vulture --min-confidence 100 -- $(git ls-files -- "*.py")) instead to make sure only files in the repo are checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be changed if people think that's better. The rev-parse version is what the previous incarnation of the linter had. I was thinking maybe you want to run the linter against a new file before you git add it?

AFAICT, vulture automatically filters by file extension (https://github.com/jendrikseipp/vulture/blob/master/vulture/config.py#L105), so *.py should be implicit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think this is a serious issue but, ostensibly a developer could, unwisely, keep their own .py scripts inside the repo directory, which would make no sense to check here, so i think ls-files to only check the committed ones makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, changed to ls-files as requested

@practicalswift
Copy link
Contributor

ACK 3f8776a

@maflcko
Copy link
Member

maflcko commented Feb 15, 2021

Tested ACK by reintroducing the bug, didn't review:

/tmp/cirrus-ci-build/test/functional/feature_taproot.py:521: unreachable code after 'return' (100% confidence)
^---- failure generated from test/lint/lint-python-dead-code.sh

@maflcko maflcko merged commit d19639d into bitcoin:master Feb 15, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 15, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Sep 10, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Sep 24, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Oct 4, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Oct 5, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
kwvg pushed a commit to kwvg/dash that referenced this pull request Oct 12, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Nov 18, 2021
3f8776a Re-add dead code detection (flack)

Pull request description:

  This re-adds unreachable code detection for Python based on `vulture`.

  Effectively, this reverts f4beb49. The difference to the previous version is that this runs with the `--min-confidence 100` setting. From https://pypi.org/project/vulture/:

  > Use `--min-confidence 100` to only report code that is guaranteed to be unused within the analyzed files.

  So this should avoid the previous issues where static analysis had wrong positives due to the dynamic nature of Python code by only reporting things that are unambiguous (such as code after a `return` statement). As such, there is not suppressions list.

  My motivation was mainly bitcoin#21081 which would have been caught by this (as can be seen by the CI run failing). This is still marked as draft because bitcoin#21081 is needed to get the linter to pass. Also, there is a second problem that this found (see https://github.com/bitcoin/bitcoin/pull/19509/files#r571454691). From what I can tell, this is a spurious type comment that could just be removed (or if that line has no side effects it could also be deleted altogether?). I could add a commit here to fix it, but I wanted to see if there is interest in having this linter again in the first place

ACKs for top commit:
  practicalswift:
    ACK 3f8776a

Tree-SHA512: 52314ad4f627d969de1eb15375ca677ed86a2e816fe773756a1ce22421214ba407b5a09a4bf701a3aab1a10c7b336f548e4cef3327edf154acba55e987db21f6
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants