This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add new modernize use unary assert check
ClosedPublic

Authored by barancsuk on Jul 11 2017, 7:02 AM.

Details

Summary

The check finds static_assert declarations with empty messages, and
replaces them with an unary static_assert declaration.

The check is only applicable for C++17 code.

The following code:

void f_textless(int a){
  static_assert(sizeof(a) <= 10, "");
}

is replaced by:

void f_textless(int a){
  static_assert(sizeof(a) <= 10 );
}

Diff Detail

Event Timeline

barancsuk created this revision.Jul 11 2017, 7:02 AM

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

clang-tidy/modernize/UnaryStaticAssertCheck.cpp
29

Please remove empty line.

docs/clang-tidy/checks/modernize-unary-static-assert.rst
16

Please Clang-format snippets. Same below.

27

Please remove empty line.

barancsuk updated this revision to Diff 106021.Jul 11 2017, 7:17 AM
  • Do not rewrite macro expansions
aaron.ballman added inline comments.Jul 11 2017, 7:18 AM
clang-tidy/modernize/UnaryStaticAssertCheck.cpp
29

Can remove the empty line.

37

I would hoist this up to the previous if statement and use !AssertMessage->getLength(). This eliminates the possibility of an assert failing because the string literal is not an ASCII string.

You should add a test with L"", which I would expect to also be replaced.

41

How about:

use unary 'static_assert' when the string literal is an empty string

clang-tidy/modernize/UnaryStaticAssertCheck.h
20

static assert -> a static_assert
with empty -> with an empty

21

with the unary version.

docs/clang-tidy/checks/modernize-unary-static-assert.rst
7–8

This mixes singular and plural a bit oddly. How about:

The check diagnoses any static_assert declaration with an empty string literal and provides a fix-it to replace the declaration with a single-argument static_assert declaration.

10

C++17 and later

barancsuk updated this revision to Diff 106025.Jul 11 2017, 8:07 AM
barancsuk marked 10 inline comments as done.
  • Address review comments
barancsuk updated this revision to Diff 106026.Jul 11 2017, 8:13 AM
  • Missed some fixes last time
aaron.ballman added inline comments.Jul 11 2017, 8:20 AM
clang-tidy/modernize/UnaryStaticAssertCheck.cpp
33

I think this should be !AssertMessage->getLength()

test/clang-tidy/modernize-unary-static-assert.cpp
19

typo: Sie

24

I think you also need to handle the case where the string literal is empty but produces from a macro expansion. e.g.,

#define MSG ""

static_assert(condition, MSG);

This should not suggest removing the empty string literal.

barancsuk marked 2 inline comments as done.
  • Further reviews addressed
barancsuk added inline comments.Jul 12 2017, 12:30 AM
clang-tidy/modernize/UnaryStaticAssertCheck.cpp
33

It works correctly without the negation, otherwise the tests fail.

aaron.ballman added inline comments.Jul 12 2017, 5:33 AM
clang-tidy/modernize/UnaryStaticAssertCheck.cpp
33

Sorry about the brain hiccup, you're correct.

test/clang-tidy/modernize-unary-static-assert.cpp
16

This probably should not diagnose, but definitely should not provide a fixit -- just because the macro is empty doesn't mean it will *always* be empty. Consider:

#if FOO
#define MSG ""
#else
#define MSG "Not empty"
#endif

I think diagnosing this case is just as likely to be a false-positive as not diagnosing, so my preference is to be silent instead of chatty. However, maybe there are other opinions.

barancsuk updated this revision to Diff 106195.Jul 12 2017, 6:27 AM
barancsuk marked 4 inline comments as done.
  • No longer warn when the message is from a macro expansion
barancsuk added inline comments.Jul 12 2017, 6:29 AM
test/clang-tidy/modernize-unary-static-assert.cpp
16

You are right. Unfortunately, the message is also from a macro expansion when the whole static assert is from a macro expansion. Maybe it is possible to check whether they are from the same macro, but I think it might better be addressed in a separate patch if there is a need to support that case.

This revision is now accepted and ready to land.Jul 12 2017, 6:29 AM
This revision was automatically updated to reflect the committed changes.