This is an archive of the discontinued LLVM Phabricator instance.

[Clang-tidy] Comparison Misuse
AbandonedPublic

Authored by xazax.hun on Aug 11 2016, 2:11 PM.

Details

Summary

This checker warns for the misuse of comparison operators

  • char* is compared to a string literal
  • inequality operator usage for NULL

Diff Detail

Repository
rL LLVM

Event Timeline

falho updated this revision to Diff 67747.Aug 11 2016, 2:11 PM
falho retitled this revision from to [Clang-tidy] Comparison Misuse.
falho updated this object.
falho set the repository for this revision to rL LLVM.
falho added a subscriber: cfe-commits.
falho added a reviewer: alex.Aug 11 2016, 2:24 PM

Please mention this check in docs/ReleaseNotes.rst (in alphabetical order).

Please run Clang-format on newly added files. Test case is definitely needs it.

clang-tidy/misc/ComparisonMisuseCheck.h
19

Check, please.

21

Spaces after commas,

docs/clang-tidy/checks/misc-comparison-misuse.rst
6

Check, please.

13

Clang-format examples, please.

Eugene.Zelenko added inline comments.Aug 11 2016, 2:40 PM
clang-tidy/misc/ComparisonMisuseCheck.cpp
21

Please remove this line.

hokein added inline comments.Aug 12 2016, 3:07 AM
clang-tidy/misc/ComparisonMisuseCheck.cpp
31

We should use nullPointerConstant() here to cover more null cases.

39

The code can be simplified as follows:

if (const auto * a = Result.Nodes.getNodeAs<BinaryOperator>("charToLiteral")) {
  ...
} else if (const auto *CompareToNull =
      Result.Nodes.getNodeAs<BinaryOperator>("compareToNull")) {
  ...
}
41

I'm wondering if we can automatically fix this case. Use strcmp() function is sufficient to fix it, IMO?

52

an extra blank line.

clang-tidy/misc/ComparisonMisuseCheck.h
20

s/should warn/warns

21

Seems like your check doesn't warn any usage about the strcmp family functions.

docs/clang-tidy/checks/misc-comparison-misuse.rst
3

Please also update docs/ReleaseNotes.rst.

10

Does the case cover char[] ?

test/clang-tidy/misc-comparison-misuse.cpp
1

Also clang-format this example.

13

Add if (p <= NULL); test case.

aaron.ballman edited edge metadata.Aug 16 2016, 6:39 AM

Thank you for working on this check!

We already have a frontend diagnostic for comparisons between string literals and pointers, so I'm not certain of the utility of adding a clang-tidy check for that case (see -Wstring-compare, aka, http://coliru.stacked-crooked.com/a/6f6ca7fd2f6db09a).

Comparisons against nullptr seems like it could also be handled as a frontend check as well, perhaps.

clang-tidy/misc/ComparisonMisuseCheck.cpp
23

Should constrain this matcher to comparison operators, no?

clang-tidy/misc/ComparisonMisuseCheck.h
21

Should remove the period at the end of this comment. Also, the w-variants for these APIs?

I don't see any code related to strcmp and friends, so perhaps this comment is spurious and should be removed?

docs/clang-tidy/checks/misc-comparison-misuse.rst
1

The code examples in the documentation should be formatted with our usual style guidelines, such as char * rather than char*, etc.

10

The check is covering any character type, including char32_t, wchar_t, etc.

test/clang-tidy/misc-comparison-misuse.cpp
1

Should be tests for other character types.

xazax.hun commandeered this revision.Feb 13 2017, 2:52 AM
xazax.hun added a reviewer: falho.
xazax.hun abandoned this revision.Feb 13 2017, 2:54 AM

For the first case ToT clang compiler gives a warning (-Wstring-compare), for the second case, it generates a compiler error (error: ordered comparison between pointer and zero). Note that, older versions of clang did not even give a warning for that case.

It looks like this check no longer makes sense considering the current warnings and errors of clang top of tree.