From 51f8fa3febd1b8209bf7c2e37770d93bfdacad03 Mon Sep 17 00:00:00 2001 From: Thorrigan Date: Fri, 10 Jan 2020 21:55:07 +0100 Subject: [PATCH] fix isse #722 whileloopsmustusebraces --- build-tools/analysis.launch | 2 +- input/while-loops-must-use-braces.xmi | 73 +++++++++++++++++++++++++++ src/main/atl/analysis.atl | 25 +++++++-- src/main/atl/codestyle.atl | 9 ++++ src/main/atl/errorProne.atl | 1 + 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 input/while-loops-must-use-braces.xmi diff --git a/build-tools/analysis.launch b/build-tools/analysis.launch index e706c2d..71c6f92 100644 --- a/build-tools/analysis.launch +++ b/build-tools/analysis.launch @@ -45,7 +45,7 @@ - + diff --git a/input/while-loops-must-use-braces.xmi b/input/while-loops-must-use-braces.xmi new file mode 100644 index 0000000..5804dd3 --- /dev/null +++ b/input/while-loops-must-use-braces.xmi @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/atl/analysis.atl b/src/main/atl/analysis.atl index 54b2f46..72b913a 100644 --- a/src/main/atl/analysis.atl +++ b/src/main/atl/analysis.atl @@ -63,6 +63,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = thisModule.extendsObject(), thisModule.unnecessaryReturn(), thisModule.useShortArrayInitializer(), + thisModule.whileBraces(), -- Design rules -- @@ -145,7 +146,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = thisModule.nonCaseLabelInSwitchStatement(), thisModule.nonStaticInitializer(), thisModule.nullAssignment(), - thisModule.simpleDateFormatNeedsLocale(), + --thisModule.simpleDateFormatNeedsLocale(), thisModule.suspiciousEqualsMethodName(), thisModule.suspiciousHashcodeMethodName(), thisModule.testClassWithoutTest(), @@ -162,7 +163,7 @@ helper def: allMeasures(project : java!Model): Set(smm!Measure) = thisModule.switchDensity(), -- #FIXME: thisModule.unusedLocalVariable(), thisModule.useAssertSameInsteadOfAssertTrue(), - thisModule.useAssertTrueInsteadOfAssertEquals() + thisModule.useAssertTrueInsteadOfAssertEquals(), -- Bugged rules: -- @@ -2421,7 +2422,25 @@ rule MesureUseAssertTrueInsteadOfAssertEquals(class : java!MethodInvocation) { } - +--------------------------------------------- WhileLoopsMustUseBraces --------------------------------------------- +--- creates a new Measure when a class that contains a while statement without braces.. +rule MeasureWhileLoopsMustUseBraces(condition : java!WhileStatement) { + to + om: smm!ObservedMeasure ( + measure <- noc, + measurements <- measurement + ), + noc: smm!DimensionalMeasure ( + name <- 'While Statement must use braces', + shortDescription <- 'Avoid using While statements without using surrounding braces.' + ), + measurement: smm!DirectMeasurement ( + error <- 'The class ' + condition.originalCompilationUnit.name + ' has a While statement without braces ' + ) + do { + noc; + } +} ------------------------------------------------------------------------------------------ diff --git a/src/main/atl/codestyle.atl b/src/main/atl/codestyle.atl index 8ac892d..be52d9f 100644 --- a/src/main/atl/codestyle.atl +++ b/src/main/atl/codestyle.atl @@ -218,3 +218,12 @@ helper def: useShortArrayInitializer() : Set(smm!Measure) = java!VariableDeclarationFragment.allInstances() -> select(i | i.initializer.oclIsTypeOf(java!ArrayCreation) = true) -> collect (i | thisModule.MeasureUseShortArrayInitializer(i)); + + +-- ------------------------------------------- WhileLoopsMustUseBraces --------------------------------------------- +--- Helper for issue WhileLoopsMustUseBraces : return a Measure for each class that contains a while statement without braces. +helper def: whileBraces() : Set(smm!Measure) = + java!WhileStatement.allInstances() + -> select(i | i.body <> OclUndefined) + -> select(i | not i.body.oclIsTypeOf(java!Block)) + -> collect(i | thisModule.MeasureWhileLoopsMustUseBraces(i)); diff --git a/src/main/atl/errorProne.atl b/src/main/atl/errorProne.atl index 4f9dd41..4a27f26 100644 --- a/src/main/atl/errorProne.atl +++ b/src/main/atl/errorProne.atl @@ -626,3 +626,4 @@ helper def: nonCaseLabelInSwitchStatement() : Set(smm!Measure) = -> select(i | i.name <> OclUndefined) -> select(i | i.name <> 'case :' and i.name <> 'default:') -> collect (i | thisModule.MeasureNonCaseLabelInSwitchStatement(i)); + -- GitLab