Skip to content

Commit

Permalink
fix cognitive complexity
Browse files Browse the repository at this point in the history
- close #2672
  • Loading branch information
guwirth committed May 3, 2024
1 parent 80181b5 commit db55652
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import org.sonar.cxx.parser.CxxKeyword;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxPunctuator;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.parser.CxxKeyword;
import org.sonar.cxx.parser.CxxPunctuator;
import org.sonar.cxx.squidbridge.api.SourceCode;

public class CxxCognitiveComplexityVisitor<G extends Grammar> extends MultiLocatitionSquidCheck<G> {
Expand Down Expand Up @@ -74,7 +74,7 @@ public class CxxCognitiveComplexityVisitor<G extends Grammar> extends MultiLocat
private static final Set<AstNodeType> SUBSCRIPTION_NODES = new HashSet<>();

static {
SUBSCRIPTION_NODES.add(CxxGrammarImpl.functionDefinition);
SUBSCRIPTION_NODES.add(CxxGrammarImpl.functionBody);
SUBSCRIPTION_NODES.addAll(Arrays.asList(DESCENDANT_TYPES));
SUBSCRIPTION_NODES.addAll(Arrays.asList(INCREMENT_TYPES));
SUBSCRIPTION_NODES.addAll(Arrays.asList(NESTING_LEVEL_TYPES));
Expand Down Expand Up @@ -102,7 +102,7 @@ public void visitNode(AstNode node) {
return;
}

if (node.is(CxxGrammarImpl.functionDefinition)) {
if (node.is(CxxGrammarImpl.functionBody)) {
complexityScopes.addFirst(new CxxComplexityScope(node.getTokenLine()));
}

Expand All @@ -129,7 +129,7 @@ public void leaveNode(AstNode node) {
return;
}

if (node.is(CxxGrammarImpl.functionDefinition)) {
if (node.is(CxxGrammarImpl.functionBody)) {
analyzeComplexity(complexityScopes.removeFirst());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ void to_regexp() throws UnsupportedEncodingException, IOException {
assertThat(testFile("src/test/resources/visitors/to_regexp.cc")).isEqualTo(20);
}

@Test
void template() throws UnsupportedEncodingException, IOException {
assertThat(testFile("src/test/resources/visitors/template.cc")).isEqualTo(0);
}

private int testFile(String fileName) throws UnsupportedEncodingException, IOException {
var tester = CxxFileTesterHelper.create(fileName, ".", "");
SourceFile sourceFile = CxxAstScanner.scanSingleInputFile(tester.asInputFile());
Expand Down
11 changes: 11 additions & 0 deletions cxx-squid/src/test/resources/visitors/template.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
template<typename T>
class host : public std::enable_shared_from_this<host<T>> {
public:
template<typename... U>
explicit host(U&&... args); // should ignore &&
host() = delete;
host(const host&) = delete;
host(host&&) = default; // should ignore && and default
host& operator=(const host&) = delete;
host& operator=(host&&) = default; // should ignore && and default
};

0 comments on commit db55652

Please sign in to comment.