add S1192 with a top-down asciidoc design.

This commit is contained in:
Nicolas Harraudeau 2020-06-23 10:40:41 +02:00
parent 2dae34cfc5
commit 3fdcdde652
9 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,11 @@
* tag::include[]
WORKING-STORAGE SECTION.
01 FIRST-NAME-HEADER PIC X(42) VALUE "Firstname: ".
PROCEDURE DIVISION.
DISPLAY FIRST-NAME-HEADER
*...
DISPLAY FIRST-NAME-HEADER
*...
DISPLAY FIRST-NAME-HEADER
* end::include[]

View File

@ -0,0 +1,3 @@
Literals with fewer than 7 characters are ignored.
Only duplications located in a `PROCEDURE DIVISION`, not those contained in copybooks are reported.

View File

@ -0,0 +1,9 @@
* tag::include[]
PROCEDURE DIVISION.
DISPLAY "Firstname: ".
*...
DISPLAY "Firstname: ".
*...
DISPLAY "Firstname: ".
* end::include[]

View File

@ -0,0 +1,11 @@
public class Main {
// tag::include[]
private static final String ACTION_1 = "action1"; // Compliant
public void run() {
prepare(ACTION_1); // Compliant
execute(ACTION_1);
release(ACTION_1);
}
// end::include[]
}

View File

@ -0,0 +1,20 @@
public class Main {
// tag::include[]
public void run() {
prepare("action1"); // Noncompliant - "action1" is duplicated 3 times
execute("action1");
release("action1");
}
@SuppressWarning("all") // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }
public String method3(String a) {
System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded
return ""; // Compliant - literal "" has less than 5 characters and is excluded
}
// end::include[]
}

19
rules/S1192/metadata.json Normal file
View File

@ -0,0 +1,19 @@
{
"title": "String literals should not be duplicated",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Linear with offset",
"linearDesc": "per duplicate instance",
"linearOffset": "2min",
"linearFactor": "2min"
},
"tags": [
"design"
],
"defaultSeverity": "Critical",
"ruleSpecification": "RSPEC-1192",
"sqKey": "S1192",
"scope": "Main",
"qualityProfiles": ["Sonar way"]
}

View File

@ -0,0 +1,9 @@
// tag::include[]
ACTION_1 = 'action1';
function run() {
prepare(ACTION_1);
execute(ACTION_1);
release(ACTION_1);
}
// end::include[]

View File

@ -0,0 +1,7 @@
// tag::include[]
function run() {
prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times
execute('action1');
release('action1');
}
// end::include[]

39
rules/S1192/rule.adoc Normal file
View File

@ -0,0 +1,39 @@
// :language: cobol
// :file_extension: cbl
// :language_group: cobol
// :cobol:
// :language: java
// :file_extension: java
// :language_group: java
// :language: php
// :language_group: php
// :file_extension: php
:source-highlighter: prettify
:source-language: {language}
:source-indent: 0
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
== Noncompliant Code Example
With the default threshold of 3:
----
include::{language_group}/noncompliant.{file_extension}[tag=include]
----
== Compliant Code Example
----
include::{language_group}/compliant.{file_extension}[tag=include]
----
== Exceptions
ifdef::cobol[]
include::{language_group}/exceptions.adoc[]
endif::[]
ifndef::cobol[]
To prevent generating some false-positives, literals having less than 5 characters are excluded.
endif::[]