Skip to content

Commit

Permalink
SONARJAVA-1755 Add parameter to only target long (#908)
Browse files Browse the repository at this point in the history
* SONARJAVA-1755 Add parameter to only target long

* Remove link to deprecated rule
  • Loading branch information
Wohops authored and benzonico committed Jun 28, 2016
1 parent db89be6 commit 997f1d0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@
package org.sonar.java.checks;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.LiteralTree;
import org.sonar.plugins.java.api.tree.Tree;

import java.util.List;
import java.util.Set;

@Rule(key = "S818")
public class UppercaseSuffixesCheck extends IssuableSubscriptionVisitor {

private static final Set<Character> LITERAL_SUFFIXES = ImmutableSet.of('f', 'd', 'l');
@RuleProperty(
key = "checkOnlyLong",
description = "Set to \"true\" to ignore \"float\" and \"double\" declarations.",
defaultValue = "false")
public boolean checkOnlyLong = false;

private static final char LONG = 'l';
private static final char DOUBLE = 'd';
private static final char FLOAT = 'f';

@Override
public List<Tree.Kind> nodesToVisit() {
Expand All @@ -43,8 +51,17 @@ public List<Tree.Kind> nodesToVisit() {
public void visitNode(Tree tree) {
String value = ((LiteralTree) tree).value();
char suffix = value.charAt(value.length() - 1);
if (LITERAL_SUFFIXES.contains(suffix)) {
reportIssue(tree, "Upper-case this literal \"" + suffix + "\" suffix.");
switch (suffix) {
case DOUBLE:
case FLOAT:
if (checkOnlyLong) {
return;
}
case LONG:
reportIssue(tree, "Upper-case this literal \"" + suffix + "\" suffix.");
break;
default:
// do nothing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,3 @@ <h2>See</h2>
</li><li> <a href="https://www.securecoding.cert.org/confluence/x/n4AtAQ">CERT DCL16-CPP</a> - Use "L," not "l," to indicate a long value
</li></ul>

<h3>See Also</h3>
<ul>
<li> {rule:squid:S1129} - Long suffix "L" should be upper case
</li></ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class A {
double f = 1.;
long long1 = 1l; // Noncompliant [[sc=16;ec=18]] {{Upper-case this literal "l" suffix.}}
float float1 = 1.0f; // Compliant
double double1 = 1.0d; // Compliant

private void test () {

long retVal = (bytes[0] & 0xFF);
for (int i = 1; i < Math.min(bytes.length, 8); i++) {
retVal |= (bytes[i] & 0xFFL) << (i * 8);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public void test() {
JavaCheckVerifier.verify("src/test/files/checks/UppercaseSuffixesCheck.java", new UppercaseSuffixesCheck());
}

@Test
public void test_only_long() {
UppercaseSuffixesCheck check = new UppercaseSuffixesCheck();
check.checkOnlyLong = true;
JavaCheckVerifier.verify("src/test/files/checks/UppercaseSuffixesCheckOnlyLong.java", check);
}

}

0 comments on commit 997f1d0

Please sign in to comment.