Bug 27333 - cpplint should check for equality comparisons to 0/true/false
Summary: cpplint should check for equality comparisons to 0/true/false
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Enhancement
Assignee: David Levin
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-15 23:54 PDT by David Levin
Modified: 2009-07-16 12:23 PDT (History)
3 users (show)

See Also:


Attachments
Proposed fix. (4.38 KB, patch)
2009-07-16 00:06 PDT, David Levin
oliver: review+
Details | Formatted Diff | Diff
Proposed fix. (5.00 KB, patch)
2009-07-16 02:48 PDT, David Levin
no flags Details | Formatted Diff | Diff
Proposed fix (that addresses previous comments and has an updated ChangeLog). (5.20 KB, patch)
2009-07-16 08:04 PDT, David Levin
no flags Details | Formatted Diff | Diff
Updated patched. (5.20 KB, patch)
2009-07-16 08:41 PDT, David Levin
ddkilzer: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Levin 2009-07-15 23:54:01 PDT
According to http://webkit.org/coding/coding-style.html,
"Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons."
Comment 1 David Levin 2009-07-16 00:06:09 PDT
Created attachment 32839 [details]
Proposed fix.
Comment 2 Shinichiro Hamaji 2009-07-16 01:24:45 PDT
Though I'm not a reviewer, it would make sense I comment on this patch as I introduced the linter. This looks great, thanks for this. A few comments:

- Maybe this rule will warn for "font == truetype" or something like this? Actually, it seems that HTMLMarqueeElement.cpp has "attr->name() == truespeedAttr".
- Could you update WebKitStyleTest.test_null_false_zero using the example of the style guide? We can see the list of supported rules by this.
Comment 3 David Levin 2009-07-16 02:48:55 PDT
Created attachment 32849 [details]
Proposed fix.
Comment 4 David Levin 2009-07-16 02:52:44 PDT
Nice catch on that bug Shinichiro.  I've fixed it and moved the test case as suggested.

I did one one other small fix to the unit tests in this bug: I changed a regex in another test because it was failing due to a number in one of my directory names (WebKit-2).
Comment 5 David Levin 2009-07-16 08:04:30 PDT
Created attachment 32872 [details]
Proposed fix (that addresses previous comments and has an updated ChangeLog).
Comment 6 David Levin 2009-07-16 08:41:46 PDT
Created attachment 32874 [details]
Updated patched.

Fixed the (ironic) incorrectly styled variable names in the test cases.
Comment 7 David Kilzer (:ddkilzer) 2009-07-16 11:40:10 PDT
Comment on attachment 32874 [details]
Updated patched.

> +def check_for_comparisons_to_zero(filename, clean_lines, line_number, error):
> +    # Get the line without comments and strings.
> +    line = clean_lines.elided[line_number]
> +
> +    # Include NULL here so that users don't have to convert NULL to 0 first and then get this error.
> +    if search(r'[=!]=\s*(NULL|0|true|false)\W', line) or search(r'\W(NULL|0|true|false)\s*[=!]=', line):
> +        error(filename, line_number, 'readability/comparison_to_zero', 5,
> +	      'Tests for true/false, zero/non-zero, etc. should be done without equality comparisons.')

I think the error message should be more specific:

      'Tests for true/false, zero/non-zero, NULL/not-NULL should be done without equality comparisons.')

> @@ -2907,8 +2904,29 @@ class WebKitStyleTest(CpplintTestBase):
>  
>          # 3. Tests for true/false, null/non-null, and zero/non-zero should
>          #    all be done without equality comparisons.
> -        # FIXME: Implement this.
> -        pass
> +        self.assert_lint(
> +            'if (count == 0)',
> +            'Tests for true/false, zero/non-zero, etc. should be done without equality comparisons.'
> +            '  [readability/comparison_to_zero] [5]')
> +        self.assert_lint(
> +            'if (string == NULL)',
> +            'Tests for true/false, zero/non-zero, etc. should be done without equality comparisons.'
> +            '  [readability/comparison_to_zero] [5]')
> +        self.assert_lint(
> +            'if (0 != aLongerVariableName)',
> +            'Tests for true/false, zero/non-zero, etc. should be done without equality comparisons.'
> +            '  [readability/comparison_to_zero] [5]')
> +        self.assert_lint(
> +            'if (condition == true)',
> +            'Tests for true/false, zero/non-zero, etc. should be done without equality comparisons.'
> +            '  [readability/comparison_to_zero] [5]')
> +        self.assert_lint(
> +            'if (myVariable == /* Why would anyone put a comment here? */ false)',
> +            'Tests for true/false, zero/non-zero, etc. should be done without equality comparisons.'
> +            '  [readability/comparison_to_zero] [5]')
> +        self.assert_lint(
> +            'if (fontType == trueType)',
> +            '')

The error messages here need to be updated to match the changed error message.

Ideally, all permutations of lhs, rhs and ==/!= should be tested here, but I think this is sufficient.

r=me with the error message change.
Comment 8 David Levin 2009-07-16 12:23:51 PDT
Committed as http://trac.webkit.org/changeset/45981