diff --git a/addons/misra.py b/addons/misra.py index 61caaadc6..4ae23ec95 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -796,10 +796,13 @@ def misra_16_2(data): def misra_16_3(rawTokens): - # state: 0=no, 1=break is seen but not its ';', 2=after 'break;', 'comment', '{' + # state: + # 0 => default state + # 1 => break/attribute is seen but not its ';' + # 2 => a case/default is allowed (we have seen 'break;'/'comment'/'{'/attribute) state = 0 for token in rawTokens: - if token.str == 'break': + if token.str == 'break' or token.str == 'return' or token.str == 'throw': state = 1 elif token.str == ';': if state == 1: @@ -813,8 +816,12 @@ def misra_16_3(rawTokens): state = 1 elif token.str == '{': state = 2 - elif token.str == 'case' and state != 2: - reportError(token, 16, 3) + elif token.str == '}': + state = 0 + elif token.str == 'case' or token.str == 'default': + if state != 2: + reportError(token, 16, 3) + state = 2 def misra_16_4(data): diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index 28a56e6fc..3df67a928 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -1,5 +1,5 @@ // To test: -// ~/cppcheck/cppcheck --dump misra-test.c && python misra.py -verify misra-test.c.dump +// ~/cppcheck/cppcheck --dump misra-test.c && python ../misra.py -verify misra-test.c.dump #include "path\file.h" // 20.2 #include /*abc*/ "file.h" // 20.3 @@ -226,6 +226,13 @@ void misra_16_3() { case 8: a=4; break; + case 9: + if (a==b) { + break; + } + case 10: // 16.3 + return; // 15.5 + case 11: default: break; } } @@ -257,9 +264,9 @@ void misra_16_6() { } switch (x) { - case 1: {break;} - case 2: {break;} - default: {break;} + case 1: break; + case 2: break; + default: break; } }