Skip to content

misc-static-assert generates incorrect fixes #24066

@llvmbot

Description

@llvmbot
Bugzilla Link 23692
Version unspecified
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

A couple of cases where a static_assert can't be used:

  1. expressions with addresses of struct members:
    =====
    $ cat q.cc
    #include

void f() {
struct {
int a;
int b;
} s;
assert(&s.b - &s.a == 1);
}
$ clang-tidy q.cc -- -std=c++11
1 warning generated.
q.cc:8:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
assert(&s.b - &s.a == 1);
^
static_assert , ""

The expression can be rewritten using offsetof(), so that it will be suitable for the static_assert. Thus the check should continue to warn, but it should provide no fixit in this case.

  1. some "not constant enough" expressions:
    =====
    $ cat q.cc
    #include

const double x = 1;
void f() {
assert(x > 0);
}
$ clang-tidy -fix q.cc -- -std=c++11
1 warning generated.
q.cc:5:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
assert(x > 0);
^
static_assert , ""
q.cc:5:3: note: FIX-IT applied suggested code changes
assert(x > 0);
^
q.cc:5:15: note: FIX-IT applied suggested code changes
assert(x > 0);
^
clang-tidy applied 2 of 2 suggested fixes.
google3$ clang-check q.cc -- -std=c++11
q.cc:5:17: error: static_assert expression is not an integral constant expression
static_assert(x > 0, "");
^~~~~
q.cc:5:17: note: read of non-constexpr variable 'x' is not allowed in a constant expression
q.cc:3:14: note: declared here
const double x = 1;
^
1 error generated.
Error while processing q.cc.

Same here. It's fine to flag this assert, maybe with a slightly different message. But the fixit shouldn't be there.

Metadata

Metadata

Assignees

Labels

awaiting-reviewHas pending Phabricator reviewbugzillaIssues migrated from bugzillaclang-tidyconfirmedVerified by a second party

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions