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

Add checker for empty comments #3870

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
Pylint's ChangeLog
------------------

* Add check for empty comments

* Fix minor documentation issue in contribute.rst


What's New in Pylint 2.6.1?
===========================
Release date: 2020-09-08
Expand Down
1 change: 1 addition & 0 deletions doc/whatsnew/2.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Summary -- Release highlights
New checkers
============

* Add `empty-comment` check for empty comments.

Other Changes
=============
Expand Down
56 changes: 56 additions & 0 deletions pylint/extensions/empty_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pylint.checkers import BaseChecker
from pylint.interfaces import IRawChecker


def is_line_commented(line):
""" Checks if a `# symbol that is not part of a string was found in line"""

comment_idx = line.find(b"#")
if comment_idx == -1:
return False
if comment_part_of_string(line, comment_idx):
return is_line_commented(line[:comment_idx] + line[comment_idx + 1 :])
return True


def comment_part_of_string(line, comment_idx):
""" checks if the symbol at comment_idx is part of a string """

if (
line[:comment_idx].count(b"'") % 2 == 1
and line[comment_idx:].count(b"'") % 2 == 1
) or (
line[:comment_idx].count(b'"') % 2 == 1
and line[comment_idx:].count(b'"') % 2 == 1
):
return True
return False


class CommentChecker(BaseChecker):
__implements__ = IRawChecker

name = "refactoring"
msgs = {
"R2044": (
"Line with empty comment",
"empty-comment",
(
"Used when a # symbol appears on a line not followed by an actual comment"
),
)
}
options = ()
priority = -1 # low priority

def process_module(self, node):
with node.stream() as stream:
for (line_num, line) in enumerate(stream):
line = line.rstrip()
if line.endswith(b"#"):
if not is_line_commented(line[:-1]):
self.add_message("empty-comment", line=line_num)


def register(linter):
linter.register_checker(CommentChecker(linter))
9 changes: 9 additions & 0 deletions tests/extensions/data/empty_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""empty-comment test-case"""
A = 5 #
#
A = '#' + '1'
print(A) #
print("A=", A) # should not be an error#
A = "#pe\0ace#love#" #
A = "peace#love" # \0 peace'#'''' love#peace'''-'#love'-"peace#love"#
#######
34 changes: 34 additions & 0 deletions tests/extensions/test_empty_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pathlib import Path

import pytest

import pylint.extensions.empty_comment as empty_comment


@pytest.fixture(scope="module")
def checker():
return empty_comment.CommentChecker


@pytest.fixture(scope="module")
def enable():
return ["empty-comment"]


@pytest.fixture(scope="module")
def disable():
return ["all"]


def test_comment_base_case(linter):
comment_test = str(Path(__file__).parent.joinpath("data", "empty_comment.py"))
linter.check([comment_test])
msgs = linter.reporter.messages
assert len(msgs) == 4
for msg in msgs:
assert msg.symbol == "empty-comment"
assert msg.msg == "Line with empty comment"
assert msgs[0].line == 1
assert msgs[1].line == 2
assert msgs[2].line == 4
assert msgs[3].line == 6