Skip to content

Commit 559a2bc

Browse files
committed
Avoid FP exceptThrowInNoThrowFunction and exceptThrowInNoexecptFunction
1 parent e39b89e commit 559a2bc

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

lib/checkexceptionsafety.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void CheckExceptionSafety::noexceptThrows()
192192
if (scope->function && scope->function->isNoExcept &&
193193
(!scope->function->noexceptArg || scope->function->noexceptArg->str() == "true")) {
194194
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
195-
if (tok->str() != "throw") {
195+
if (tok->str() == "throw") {
196196
noexceptThrowError(tok);
197197
}
198198
}
@@ -213,7 +213,7 @@ void CheckExceptionSafety::nothrowThrows()
213213
// onlycheck throw() functions
214214
if (scope->function && scope->function->isThrow && !scope->function->throwArg) {
215215
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
216-
if (tok->str() != "throw") {
216+
if (tok->str() == "throw") {
217217
nothrowThrowError(tok);
218218
}
219219
}

test/testexceptionsafety.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,20 @@ class TestExceptionSafety : public TestFixture {
317317
"void func3() noexcept(false) { throw 1; }\n");
318318
ASSERT_EQUALS("[test.cpp:1]: (error) Exception thrown in noexcept function.\n"
319319
"[test.cpp:2]: (error) Exception thrown in noexcept function.\n", errout.str());
320+
321+
// avoid false positives
322+
check("const char *func() noexcept { return 0; }\n");
323+
ASSERT_EQUALS("", errout.str());
320324
}
321325

322326
void nothrowThrow() {
323327
check("void func1() throw() { throw 1; }\n"
324328
"void func2() throw(int) { throw 1; }\n");
325329
ASSERT_EQUALS("[test.cpp:1]: (error) Exception thrown in throw() function.\n", errout.str());
330+
331+
// avoid false positives
332+
check("const char *func() throw() { return 0; }\n");
333+
ASSERT_EQUALS("", errout.str());
326334
}
327335
};
328336

0 commit comments

Comments
 (0)