diff --git a/rules/S1007/cfamily/comments-and-links.adoc b/rules/S1007/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..06e0de6886 --- /dev/null +++ b/rules/S1007/cfamily/comments-and-links.adoc @@ -0,0 +1,3 @@ +=== on 21 Oct 2014, 13:15:09 Samuel Mercier wrote: +Cannot be implemented, as this requires understanding of the developer's intent. + diff --git a/rules/S1007/cfamily/metadata.json b/rules/S1007/cfamily/metadata.json new file mode 100644 index 0000000000..ef69dfd4d6 --- /dev/null +++ b/rules/S1007/cfamily/metadata.json @@ -0,0 +1,22 @@ +{ + "title": "When the absolute positioning of bits representing a bit-field is required, then the behaviour and packing of bit-fields shall be documented", + "type": "CODE_SMELL", + "status": "closed", + "tags": [ + "misra" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "ruleSpecification": "RSPEC-1007", + "sqKey": "S1007", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1007/cfamily/rule.adoc b/rules/S1007/cfamily/rule.adoc new file mode 100644 index 0000000000..0994c836d4 --- /dev/null +++ b/rules/S1007/cfamily/rule.adoc @@ -0,0 +1,28 @@ +Certain aspects of bit-fields are implementation-defined. In particular, the developer should be aware of the following: + +* It is implementation-defined whether the bit-fields are allocated from the high or low end of a storage unit (usually a byte). +* It is implementation-defined whether or not a bit field can overlap a storage unit boundary (e.g. if a 6-bit bit-field and a 4-bit bit-field are declared in that order, then the 4-bit bit-field may either start a new byte or it may use 2 bits in one byte and 2 bits in the next). + +These issues are generally benign (e.g. when packing together short-length data to save storage space), but they may lead to errors if the absolute position of the bit-fields is important (e.g. when accessing hardware registers). + + +Provided the elements of the structure are only accessed by name, the developer need make no assumptions about the way that the bit fields are stored within the structure. + + +Note that Rule 3-9-2 need not be followed when defining bit-fields, as their lengths are explicitly specified. + + +If the compiler has a switch to force bit fields to follow a particular layout, then this option should be documented. + + +== See Also + +* MISRA {cpp}:2008, 3-9-2 + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1007/metadata.json b/rules/S1007/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1007/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1008/cfamily/comments-and-links.adoc b/rules/S1008/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..ee41bc2b85 --- /dev/null +++ b/rules/S1008/cfamily/comments-and-links.adoc @@ -0,0 +1,8 @@ +=== duplicates: S814 + +=== on 9 Dec 2014, 21:11:36 Evgeny Mandrikov wrote: +\[~ann.campbell.2] I'd like to close this as duplicate of RSPEC-814, however they are significantly different in definition of SQALE model (characteristic and cost). So could you please advise which of those two SQALE models should be used? + +=== on 9 Dec 2014, 21:12:36 Evgeny Mandrikov wrote: +Also they are different in default severity and activation. + diff --git a/rules/S1008/cfamily/metadata.json b/rules/S1008/cfamily/metadata.json new file mode 100644 index 0000000000..07ffcd169a --- /dev/null +++ b/rules/S1008/cfamily/metadata.json @@ -0,0 +1,27 @@ +{ + "title": "Bit-fields shall be either bool type or an explicitly unsigned or signed integral type", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10mn" + }, + "tags": [ + "misra" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Critical", + "ruleSpecification": "RSPEC-1008", + "sqKey": "S1008", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1008/cfamily/rule.adoc b/rules/S1008/cfamily/rule.adoc new file mode 100644 index 0000000000..af56b4b425 --- /dev/null +++ b/rules/S1008/cfamily/rule.adoc @@ -0,0 +1,40 @@ +Using ``++int++`` is implementation-defined because bit-fields of type ``++int++`` can be either ``++signed++`` or ``++unsigned++``. + + +The use of ``++wchar_t++`` as a bit-field type is prohibited as ISO/IEC 14882:2003 does not explicitly define the underlying representation as ``++signed++`` or ``++unsigned++``. + + +== Noncompliant Code Example + +---- +struct S +{ + char c : 2; // Noncompliant + int i : 2; // Noncompliant + short f : 2; // Noncompliant + wchar_t k : 2; // Noncompliant + signed int a : 2; // Compliant + unsigned int b : 2; // Compliant + signed char d : 2; // Compliant + unsigned char e : 2; // Compliant + signed short g : 2; // Compliant + unsigned short h : 2; // Compliant + bool j : 2; // Compliant + uint32_t l : 2; // Compliant + int8_t m : 2; // Compliant +}; +---- + + +== See + +* MISRA {cpp}:2008, 9-6-2 +* ISO/IEC 14882:2003 + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1008/metadata.json b/rules/S1008/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1008/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1010/cfamily/comments-and-links.adoc b/rules/S1010/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..4aa276b254 --- /dev/null +++ b/rules/S1010/cfamily/comments-and-links.adoc @@ -0,0 +1,2 @@ +=== duplicates: S2216 + diff --git a/rules/S1010/cfamily/metadata.json b/rules/S1010/cfamily/metadata.json new file mode 100644 index 0000000000..0b942df91d --- /dev/null +++ b/rules/S1010/cfamily/metadata.json @@ -0,0 +1,28 @@ +{ + "title": "Named bit-fields with signed integer type should have a length of more than one bit", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "misra", + "suspicious" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1010", + "sqKey": "S1010", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1010/cfamily/rule.adoc b/rules/S1010/cfamily/rule.adoc new file mode 100644 index 0000000000..73725e289c --- /dev/null +++ b/rules/S1010/cfamily/rule.adoc @@ -0,0 +1,38 @@ +The values which may be represented by a bit-field of length one may not meet developer expectations. Anonymous signed bit-fields of any length are allowed. + + +== Noncompliant Code Example + +---- +struct S +{ + signed int a : 1; // Noncompliant, signed fields require at least two bits + signed int : 1; // Compliant, cannot be referenced + signed int : 0; // Compliant, cannot be referenced +}; +---- + + +== Compliant Solution + +---- +struct S +{ + signed int a : 2; // Compliant + signed int : 1; // Compliant, cannot be referenced + signed int : 0; // Compliant, cannot be referenced +}; +---- + + +== See + +* MISRA {cpp}:2008, 9-6-4 + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1010/metadata.json b/rules/S1010/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1010/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1018/cfamily/comments-and-links.adoc b/rules/S1018/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..a607478f13 --- /dev/null +++ b/rules/S1018/cfamily/comments-and-links.adoc @@ -0,0 +1,2 @@ +=== duplicates: S3656 + diff --git a/rules/S1018/cfamily/metadata.json b/rules/S1018/cfamily/metadata.json new file mode 100644 index 0000000000..fe394e79e9 --- /dev/null +++ b/rules/S1018/cfamily/metadata.json @@ -0,0 +1,27 @@ +{ + "title": "Member data in non-POD class types should be private", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "misra" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1018", + "sqKey": "S1018", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1018/cfamily/rule.adoc b/rules/S1018/cfamily/rule.adoc new file mode 100644 index 0000000000..4249669bc1 --- /dev/null +++ b/rules/S1018/cfamily/rule.adoc @@ -0,0 +1,48 @@ +By implementing class interfaces with member functions, the implementation retains more control over how the object state can be modified, and helps to allow a class to be maintained without affecting clients. + + +== Noncompliant Code Example + +---- +class C +{ +public: + int32_t b; // Noncompliant +protected: + int32_t c; // Noncompliant +private: + int32_t d; // Compliant +}; +---- + + +== Compliant Solution + +---- +class C +{ +public: + int32_t getB() { return _b; } + void setB(int32_t b) { _b = b; } +protected: + int32_t getC() { return _c; } + void setC(int32_t c) { _c = c; } +private: + int32_t _b; // Compliant + int32_t _c; // Compliant + int32_t _d; // Compliant +}; +---- + + +== See + +* MISRA {cpp}:2008, 11-0-1 + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1018/metadata.json b/rules/S1018/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1018/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1021/cfamily/comments-and-links.adoc b/rules/S1021/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..89bb9e6866 --- /dev/null +++ b/rules/S1021/cfamily/comments-and-links.adoc @@ -0,0 +1,8 @@ +=== duplicates: S1709 + +=== on 17 Nov 2015, 21:49:14 Evgeny Mandrikov wrote: +\[~ann.campbell.2] duplicates RSPEC-1709 ? + +=== on 18 Nov 2015, 17:38:58 Ann Campbell wrote: +It sure does [~evgeny.mandrikov]. Closed & MISRA reference added to RSPEC-1709 + diff --git a/rules/S1021/cfamily/metadata.json b/rules/S1021/cfamily/metadata.json new file mode 100644 index 0000000000..b4450c7fb6 --- /dev/null +++ b/rules/S1021/cfamily/metadata.json @@ -0,0 +1,27 @@ +{ + "title": "Constructors that are callable with a single argument of fundamental type should be \"explicit\"", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10mn" + }, + "tags": [ + "misra" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1021", + "sqKey": "S1021", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1021/cfamily/rule.adoc b/rules/S1021/cfamily/rule.adoc new file mode 100644 index 0000000000..4a485de83f --- /dev/null +++ b/rules/S1021/cfamily/rule.adoc @@ -0,0 +1,40 @@ +The explicit keyword prevents the constructor from being used to implicitly convert from a fundamental type to the class type. + + +== Noncompliant Code Example + +---- +class C +{ +public: + C ( int32_t a ) // Noncompliant + { + } +}; +---- + + +== Compliant Solution + +---- +class D +{ +public: + explicit D ( int32_t a ) + { + } +}; +---- + + +== See + +* MISRA {cpp}:2008, 12-1-3 + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1021/metadata.json b/rules/S1021/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1021/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1038/cfamily/comments-and-links.adoc b/rules/S1038/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..ab6d4254d2 --- /dev/null +++ b/rules/S1038/cfamily/comments-and-links.adoc @@ -0,0 +1,6 @@ +=== on 16 Aug 2016, 18:06:27 Alban Auzeill wrote: +I close this rule in favor of [RSPEC-3696] that mark as non-compliant NULL and 0 (zero). + +=== on 16 Aug 2016, 19:28:50 Ann Campbell wrote: +As an addendum to the previous comment: ``++NULL++`` evaluates to 0, so we can't distinguish between ``++throw NULL++`` and ``++throw 0++``. + diff --git a/rules/S1038/cfamily/metadata.json b/rules/S1038/cfamily/metadata.json new file mode 100644 index 0000000000..cf372cbd14 --- /dev/null +++ b/rules/S1038/cfamily/metadata.json @@ -0,0 +1,28 @@ +{ + "title": "NULL should not be thrown", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + "misra", + "pitfall" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1038", + "sqKey": "S1038", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1038/cfamily/rule.adoc b/rules/S1038/cfamily/rule.adoc new file mode 100644 index 0000000000..381f765445 --- /dev/null +++ b/rules/S1038/cfamily/rule.adoc @@ -0,0 +1,50 @@ +``++throw(NULL)++`` is equivalent to ``++throw(0)++``, and is therefore caught by an integer handler. However, since ``++NULL++`` is typically used in the context of pointers, developers may expect it to be caught by a pointer-to-type handler. Thus to avoid confusion, zero should be thrown instead of ``++NULL++``. + + +== Noncompliant Code Example + +---- +try +{ + throw ( NULL ); // Noncompliant +} +catch ( int32_t i ) // NULL exception handled here +{ + // ... +} +catch ( const char_t * ) // Developer may expect it to be caught here +{ + // ... +} +---- + + +== Compliant Solution + +---- +try +{ + throw ( 0 ); +} +catch ( int32_t i ) +{ + // ... +} +catch ( const char_t * ) +{ + // ... +} +---- + + +== See + +* MISRA {cpp}:2008, 15-1-2 + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1038/metadata.json b/rules/S1038/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1038/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1049/cfamily/comments-and-links.adoc b/rules/S1049/cfamily/comments-and-links.adoc new file mode 100644 index 0000000000..54a0282e83 --- /dev/null +++ b/rules/S1049/cfamily/comments-and-links.adoc @@ -0,0 +1,12 @@ +=== relates to: S1047 + +=== relates to: S1050 + +=== on 8 Nov 2018, 14:58:39 Loïc Joly wrote: +Exceptions specifications have been deprecated in {cpp}11, and removed in {cpp}17/{cpp}20. And even before that, it was never an accepted practice. + + +Therefore, canceling this RSPEC. + + + diff --git a/rules/S1049/cfamily/metadata.json b/rules/S1049/cfamily/metadata.json new file mode 100644 index 0000000000..4ae2ea50e4 --- /dev/null +++ b/rules/S1049/cfamily/metadata.json @@ -0,0 +1,37 @@ +{ + "title": "Functions should not throw exceptions not included in their specifications", + "type": "BUG", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "cwe", + "misra", + "cert" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Blocker", + "ruleSpecification": "RSPEC-1049", + "sqKey": "S1049", + "scope": "Main", + "securityStandards": { + "CWE": [ + 391 + ], + "CERT": [ + "ERR55-CPP." + ] + }, + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1049/cfamily/rule.adoc b/rules/S1049/cfamily/rule.adoc new file mode 100644 index 0000000000..939ab2c47d --- /dev/null +++ b/rules/S1049/cfamily/rule.adoc @@ -0,0 +1,52 @@ +When exception types are included in a method specification, only those exception types may be thrown by the method. If an attempt is made to throw anything else, then by default a ``++std::bad_exception++`` is thrown. If ``++std::bad_exception++`` is not itself listed in the method specification, then the end result is that ``++terminate()++`` is called, resulting in an implementation-defined termination of the program. + + +Methods that don't include exception types in their specifications can throw any exception type. However, this fact should not be taken as an argument for omitting exception types. It is far better to thoroughly specify a method, so that callers know what to expect, than to leave them in the dark. + + +Therefore, all exceptions that could be thrown by a method should be explicitly listed in its specification. + + +== Noncompliant Code Example + +---- +void foo () // no exceptions specified +{ + throw ( 21 ); // anything can be thrown +} + +void goo ( ) throw ( Exception ) +{ + foo ( ); // Noncompliant; an int could be thrown +} +---- + + +== Compliant Solution + +---- +void foo () // no exceptions specified +{ + throw ( 21 ); // this is legal; anything can be thrown +} + +void goo ( ) throw ( Exception, int ) +{ + foo ( ); +} +---- + + +== See + +* MISRA {cpp}:2008, 15-5-2 +* http://cwe.mitre.org/data/definitions/391.html[MITRE, CWE-391] - Unchecked Error Condition +* https://www.securecoding.cert.org/confluence/x/EADTAQ[CERT, ERR55-CPP.] - Honor exception specifications + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1049/metadata.json b/rules/S1049/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1049/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1063/metadata.json b/rules/S1063/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1063/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1063/plsql/comments-and-links.adoc b/rules/S1063/plsql/comments-and-links.adoc new file mode 100644 index 0000000000..7bdcc2c484 --- /dev/null +++ b/rules/S1063/plsql/comments-and-links.adoc @@ -0,0 +1,5 @@ +=== duplicates: S1524 + +=== on 21 May 2013, 14:11:55 Fabrice Bellingard wrote: +Implementation: \http://jira.sonarsource.com/browse/PLSQL-348 + diff --git a/rules/S1063/plsql/metadata.json b/rules/S1063/plsql/metadata.json new file mode 100644 index 0000000000..9f73ee5ebc --- /dev/null +++ b/rules/S1063/plsql/metadata.json @@ -0,0 +1,27 @@ +{ + "title": "Names should not be reused in inner scopes", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "30min" + }, + "tags": [ + "pitfall" + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1063", + "sqKey": "S1063", + "scope": "Main", + "defaultQualityProfiles": [ + "Sonar way" + ] +} diff --git a/rules/S1063/plsql/rule.adoc b/rules/S1063/plsql/rule.adoc new file mode 100644 index 0000000000..22caedcb45 --- /dev/null +++ b/rules/S1063/plsql/rule.adoc @@ -0,0 +1,48 @@ +Using the same name for multiple purposes reduces the understandability of the code and might eventually lead to bugs. + +This rule verifies that no name is reused in an inner scope. + + +== Noncompliant Code Example + +---- +<> -- Compliant +DECLARE + a CONSTANT PLS_INTEGER := 0; +BEGIN + <> -- Non-Compliant + DECLARE + b CONSTANT PLS_INTEGER := 42; + BEGIN + DBMS_OUTPUT.PUT_LINE('x = ' || foo.b); -- Confusing + END; +END; +/ +---- + + +== Compliant Solution + +---- +<> -- Compliant +DECLARE + a CONSTANT PLS_INTEGER := 0; +BEGIN + <> -- Compliant + DECLARE + b CONSTANT PLS_INTEGER := 42; + BEGIN + DBMS_OUTPUT.PUT_LINE('x = ' || bar.b); -- Clear + END; +END; +/ +---- + + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1073/metadata.json b/rules/S1073/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S1073/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S1073/plsql/comments-and-links.adoc b/rules/S1073/plsql/comments-and-links.adoc new file mode 100644 index 0000000000..bfb543fe7e --- /dev/null +++ b/rules/S1073/plsql/comments-and-links.adoc @@ -0,0 +1,8 @@ +=== duplicates: S2145 + +=== on 30 May 2013, 13:48:23 Fabrice Bellingard wrote: +This is originally a TOAD rule. + + +It could be improve to check only the first case which involves a single variable (because the gain in readability of the 2nd case is not really clear...). + diff --git a/rules/S1073/plsql/metadata.json b/rules/S1073/plsql/metadata.json new file mode 100644 index 0000000000..71ef949d8d --- /dev/null +++ b/rules/S1073/plsql/metadata.json @@ -0,0 +1,27 @@ +{ + "title": "Complex \"IF\" statements should be replaced by \"CASE\" statements ", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-1073", + "sqKey": "S1073", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1073/plsql/rule.adoc b/rules/S1073/plsql/rule.adoc new file mode 100644 index 0000000000..ce6f5ba567 --- /dev/null +++ b/rules/S1073/plsql/rule.adoc @@ -0,0 +1,78 @@ +Complex chains of IF, ELSIF and ELSE statements should be replaced by the more readable CASE one. A complex IF statement has either several ELSIF clauses, or both an ELSIF and an ELSE clause. + + +== Noncompliant Code Example + +---- +DECLARE + x PLS_INTEGER := 0; +BEGIN + IF x = 0 THEN -- Noncompliant + DBMS_OUTPUT.PUT_LINE('x = 0'); + ELSIF x = 1 THEN + DBMS_OUTPUT.PUT_LINE('x = 1'); + ELSIF x = 2 THEN + DBMS_OUTPUT.PUT_LINE('x = 2'); + END IF; +END; +/ + +DECLARE + x PLS_INTEGER := 0; + y PLS_INTEGER := 0; +BEGIN + IF x = 0 THEN -- Noncompliant + DBMS_OUTPUT.PUT_LINE('x = 0, y = ?'); + ELSIF y = 1 THEN + DBMS_OUTPUT.PUT_LINE('x != 0, y = 1'); + ELSE + DBMS_OUTPUT.PUT_LINE('x != 0, y != 1'); + END IF; +END; +/ +---- + + +== Compliant Solution + +---- +DECLARE + x PLS_INTEGER := 0; +BEGIN + CASE x + WHEN 0 THEN + DBMS_OUTPUT.PUT_LINE('x = 0'); + WHEN 1 THEN + DBMS_OUTPUT.PUT_LINE('x = 1'); + WHEN 2 THEN + DBMS_OUTPUT.PUT_LINE('x = 2'); + ELSE + -- Do not forget the ELSE to prevent ORA-06592 + NULL; + END CASE; +END; +/ + +DECLARE + x PLS_INTEGER := 0; + y PLS_INTEGER := 0; +BEGIN + CASE -- Compliant + WHEN x = 0 THEN + DBMS_OUTPUT.PUT_LINE('x = 0, y = ?'); + WHEN y = 1 THEN + DBMS_OUTPUT.PUT_LINE('x != 0, y = 1'); + ELSE + DBMS_OUTPUT.PUT_LINE('x != 0, y != 1'); + END CASE; +END; +/ +---- + + +ifdef::env-github,rspecator-view[] +== Comments And Links +(visible only on this page) + +include::comments-and-links.adoc[] +endif::env-github,rspecator-view[] diff --git a/rules/S1076/comments-and-links.adoc b/rules/S1076/comments-and-links.adoc new file mode 100644 index 0000000000..5a66395890 --- /dev/null +++ b/rules/S1076/comments-and-links.adoc @@ -0,0 +1,11 @@ +=== is related to: S1149 + +=== on 8 Aug 2013, 06:05:30 Dinesh Bolkensteyn wrote: +Perhaps this rule should be merged with RSPEC-1149, as it's again about synchronized classes. + +=== on 8 Aug 2013, 06:19:35 Dinesh Bolkensteyn wrote: +Implemented by \https://jira.codehaus.org/browse/SONARJAVA-179 + +=== on 8 Aug 2013, 12:36:43 Freddy Mallet wrote: +This rule spec has been merged with RSPEC-1149 + diff --git a/rules/S1076/metadata.json b/rules/S1076/metadata.json new file mode 100644 index 0000000000..3ba8b27ffc --- /dev/null +++ b/rules/S1076/metadata.json @@ -0,0 +1,28 @@ +{ + "title": "StringBuilder class should be used in place of StringBuffer class", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Linear", + "linearDesc": null, + "linearFactor": "10mn" + }, + "tags": [ + + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1076", + "sqKey": "S1076", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1076/rule.adoc b/rules/S1076/rule.adoc new file mode 100644 index 0000000000..6b509e6bf7 --- /dev/null +++ b/rules/S1076/rule.adoc @@ -0,0 +1,4 @@ +Since Java 5, StringBuffer has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. + +StringBuilder is faster, as it performs no synchronization, and should be preferred. + diff --git a/rules/S1078/comments-and-links.adoc b/rules/S1078/comments-and-links.adoc new file mode 100644 index 0000000000..627bf4baa5 --- /dev/null +++ b/rules/S1078/comments-and-links.adoc @@ -0,0 +1,3 @@ +=== on 8 Jul 2013, 18:26:58 Freddy Mallet wrote: +Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-2997 + diff --git a/rules/S1078/metadata.json b/rules/S1078/metadata.json new file mode 100644 index 0000000000..4da77f1e87 --- /dev/null +++ b/rules/S1078/metadata.json @@ -0,0 +1,28 @@ +{ + "title": "Form fields should be associated to labels", + "type": "CODE_SMELL", + "status": "closed", + "remediation": { + "func": "Linear", + "linearDesc": null, + "linearFactor": null + }, + "tags": [ + + ], + "extra": { + "coveredLanguages": [ + + ], + "replacementRules": [ + "RSPEC-1097" + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-1078", + "sqKey": "S1078", + "scope": "Main", + "defaultQualityProfiles": [ + + ] +} diff --git a/rules/S1078/rule.adoc b/rules/S1078/rule.adoc new file mode 100644 index 0000000000..39b88cc610 --- /dev/null +++ b/rules/S1078/rule.adoc @@ -0,0 +1,11 @@ +The