forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyUnusedCodeBear.py
30 lines (24 loc) · 1.07 KB
/
PyUnusedCodeBear.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import autoflake
from coalib.bears.LocalBear import LocalBear
from coalib.bears.requirements.PipRequirement import PipRequirement
from coalib.results.Diff import Diff
from coalib.results.Result import Result
class PyUnusedCodeBear(LocalBear):
LANGUAGES = {"Python", "Python 2", "Python 3"}
REQUIREMENTS = {PipRequirement('autoflake', '0.6.*')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Unused Code'}
def run(self, filename, file):
"""
Detects unused code. This functionality is limited to:
- Unneeded pass statements.
- Unneeded builtin imports. (Others might have side effects.)
"""
corrected = autoflake.fix_code(''.join(file)).splitlines(True)
for diff in Diff.from_string_arrays(file, corrected).split_diff():
yield Result(self,
"This file contains unused source code.",
affected_code=(diff.range(filename),),
diffs={filename: diff})