Affects PMD Version: 6.5.0
Rule: category/java/design.xml/CyclomaticComplexity
Description: CyclomaticComplexityRule counts conditional ternary operator twice. I believe this is incorrect, as it just adds one code path and can be rewritten as an if-else statement which just has complexity 1.
Code Sample demonstrating the issue:
public void get() {
final String a = b == null ? c : d;
}
Running PMD through: CLI
Example of output:
15:07:55 E:\dev\pmd-cyclox>more TernaryOperator.java & echo --- & more ruleset-for-cyclox.xml & echo --- & pmd-bin-6.5.0\bin\pmd.bat -f xml -R ruleset-for-cyclo
x.xml -d TernaryOperator.java
class MyClass {
public void run(String b, String c) {
final String a = b == null ? c : b;
}
}
---
<?xml version="1.0"?>
<ruleset name="Custom Rules"
xmlns="http://pmd.sourceforge.net/ruleset/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/3.0.0 http://pmd.sourceforge.net/ruleset_3_0_0.xsd">
<description>
Custom rules
</description>
<rule
ref="category/java/design.xml/CyclomaticComplexity"
>
<properties>
<property name="methodReportLevel" value="1"/>
</properties>
</rule>
</ruleset>
---
jun 28, 2018 3:07:56 PM net.sourceforge.pmd.PMD processFiles
WARNING: This analysis could be faster, please consider using Incremental Analysis: https://pmd.github.io/pmd-6.5.0/pmd_userdocs_incremental_analysis.html
<?xml version="1.0" encoding="UTF-8"?>
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd"
version="6.5.0" timestamp="2018-06-28T15:07:56.983">
<file name="E:\dev\pmd-cyclox\TernaryOperator.java">
<violation beginline="2" endline="4" begincolumn="10" endcolumn="3" rule="CyclomaticComplexity" ruleset="Design" class="MyClass" method="run" externalInfoUrl="h
ttps://pmd.github.io/pmd/pmd_rules_java_design.html#cyclomaticcomplexity" priority="3">
The method 'run(String, String)' has a cyclomatic complexity of 3.
</violation>
</file>
</pmd>
PMD reports a complexity of 3, where I believe it should be 2: one for the method entry, and one for the ?-operator.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4