From 4d8a4567d60c99d33e4ac4db09c5d656b32f620a Mon Sep 17 00:00:00 2001 From: Marharyta Nedzelska Date: Thu, 11 Jul 2024 15:00:51 +0200 Subject: [PATCH] Add missing links for Dart rules --- rules/S106/dart/rule.adoc | 3 +- rules/S1065/dart/rule.adoc | 1 + rules/S1068/dart/rule.adoc | 3 ++ rules/S1116/dart/metadata.json | 16 +--------- rules/S1116/dart/rule.adoc | 27 ++++++++++------ rules/S1123/dart/rule.adoc | 4 +++ rules/S1128/dart/rule.adoc | 2 ++ rules/S1143/dart/rule.adoc | 56 +++++++++++++++++++--------------- rules/S1161/dart/rule.adoc | 4 +++ rules/S1185/dart/rule.adoc | 10 ++++-- rules/S1206/dart/rule.adoc | 18 +++++------ rules/S121/dart/rule.adoc | 4 +++ rules/S1481/dart/rule.adoc | 4 +++ rules/S1874/dart/rule.adoc | 2 ++ rules/S6531/dart/rule.adoc | 7 +++-- rules/S6535/dart/metadata.json | 2 +- rules/S6535/dart/rule.adoc | 5 +++ 17 files changed, 102 insertions(+), 66 deletions(-) diff --git a/rules/S106/dart/rule.adoc b/rules/S106/dart/rule.adoc index acea3f96ca..614893be9f 100644 --- a/rules/S106/dart/rule.adoc +++ b/rules/S106/dart/rule.adoc @@ -12,7 +12,7 @@ The following noncompliant code: [source,dart,diff-id=1,diff-type=noncompliant] ---- -void f(int x) { +void doSomething(int x) { // ... print('debug: $x'); // ... @@ -61,3 +61,4 @@ void doSomething(int x) * OWASP - https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/[Top 10 2021 Category A9 - Security Logging and Monitoring Failures] * OWASP - https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure[Top 10 2017 Category A3 - Sensitive Data Exposure] +* Dart Linter - https://dart.dev/tools/linter-rules/avoid_print[Dart Linter - avoid_print] diff --git a/rules/S1065/dart/rule.adoc b/rules/S1065/dart/rule.adoc index 3e8b6a5235..70a1aa7c61 100644 --- a/rules/S1065/dart/rule.adoc +++ b/rules/S1065/dart/rule.adoc @@ -28,3 +28,4 @@ void foo() { == Resources * https://wiki.sei.cmu.edu/confluence/x/5dUxBQ[CERT, MSC12-C.] - Detect and remove code that has no effect or is never executed +* https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=unnecessary_cast#unused_label[Dart Linter - unused label] \ No newline at end of file diff --git a/rules/S1068/dart/rule.adoc b/rules/S1068/dart/rule.adoc index b01de0343d..ecde6b6dc2 100644 --- a/rules/S1068/dart/rule.adoc +++ b/rules/S1068/dart/rule.adoc @@ -17,3 +17,6 @@ If the field isn't needed, then remove it. If the field was intended to be used, then add the missing code. +== Resources + +* https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=unnecessary_cast#unused_field[Dart compiler diagnostic - unused field] diff --git a/rules/S1116/dart/metadata.json b/rules/S1116/dart/metadata.json index 40a6092da3..0db3279e44 100644 --- a/rules/S1116/dart/metadata.json +++ b/rules/S1116/dart/metadata.json @@ -1,17 +1,3 @@ { - "tags": [ - "based-on-misra", - "cert", - "unused" - ], - "defaultQualityProfiles": [ - "Sonar way" - ], - "securityStandards": { - "CERT": [ - "MSC12-C.", - "MSC51-J.", - "EXP15-C." - ] - } + } diff --git a/rules/S1116/dart/rule.adoc b/rules/S1116/dart/rule.adoc index 14ab153bc2..1a6876e608 100644 --- a/rules/S1116/dart/rule.adoc +++ b/rules/S1116/dart/rule.adoc @@ -6,31 +6,38 @@ include::../description.adoc[] ==== Noncompliant code example -[source,cpp,diff-id=1,diff-type=noncompliant] +[source,dart,diff-id=1,diff-type=noncompliant] ---- void doSomething() { ; // Noncompliant - was used as a kind of TODO marker } +---- -if (complicated.expression.foo()); // Noncompliant - the condition doesn't make sense - bar(); +[source,dart,diff-id=2,diff-type=noncompliant] +---- +void f() { + if (complicated.expression.foo()); // Noncompliant - the condition doesn't make sense + bar(); +} ---- ==== Compliant solution -[source,cpp,diff-id=1,diff-type=compliant] +[source,dart,diff-id=1,diff-type=compliant] ---- void doSomething() { } +---- -if (complicated.expression.foo()) { - bar(); +[source,dart,diff-id=2,diff-type=compliant] +---- +void f() { + if (complicated.expression.foo()) { + bar(); + } } ---- == Resources -=== Documentation - -* https://wiki.sei.cmu.edu/confluence/x/5dUxBQ[CERT, MSC12-C.] - Detect and remove code that has no effect or is never executed -* https://wiki.sei.cmu.edu/confluence/x/WtYxBQ[CERT, EXP15-C.] - Do not place a semicolon on the same line as an if, for, or while statement +* https://dart.dev/tools/linter-rules/empty_statements[Dart Lint rule] diff --git a/rules/S1123/dart/rule.adoc b/rules/S1123/dart/rule.adoc index 684afe2167..218b375125 100644 --- a/rules/S1123/dart/rule.adoc +++ b/rules/S1123/dart/rule.adoc @@ -23,3 +23,7 @@ will be removed on or after the 4.0.0 release. """) void oldFunction(arg1, arg2) {} ---- + +== Resources + +* https://dart.dev/tools/linter-rules/provide_deprecation_message[Dart Lint rule] \ No newline at end of file diff --git a/rules/S1128/dart/rule.adoc b/rules/S1128/dart/rule.adoc index 0523f487f2..3e81d2b5b8 100644 --- a/rules/S1128/dart/rule.adoc +++ b/rules/S1128/dart/rule.adoc @@ -37,6 +37,8 @@ void main(List arguments) { == Resources +* https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=unnecessary_cast#unused_import[Dart compiler diagnostic] + === Related rules * S1481 - Unused local variables should be removed diff --git a/rules/S1143/dart/rule.adoc b/rules/S1143/dart/rule.adoc index 93e5d69393..85d39ced99 100644 --- a/rules/S1143/dart/rule.adoc +++ b/rules/S1143/dart/rule.adoc @@ -10,51 +10,54 @@ This rule raises an issue when a jump statement (``++break++``, ``++continue++`` [source,dart] ---- -class BadReturn { - double nonCompliantMethod() { - try { - return 1 / 0; - } catch (e) { - print(e); - } finally { - return 1.0; // Noncompliant +class ReturnInFinally { + int nonCompliantMethod(int n) { + for (int i = 0; i < n; ++i) { + try { + functionThrowingException(i); + } catch (e) { + print(e); + } finally { + return 1; // Noncompliant + } } + return 0; } } ---- [source,dart] ---- -class BadContinue { - double nonCompliantMethod() { - for (var o in [1, 2]) { +class ContinueInFinally { + int nonCompliantMethod(int n) { + for (int i = 0; i < n; ++i) { try { - print(o / 0); + functionThrowingException(i); } catch (e) { print(e); } finally { continue; // Noncompliant } } - return 1.0; + return 0; } } ---- [source,dart] ---- -class BadBreak { - double nonCompliantMethod() { - for (var o in [1, 2]) { +class BreakInFinally { + int nonCompliantMethod(int n) { + for (int i = 0; i < n; ++i) { try { - print(o / 0); + functionThrowingException(i); } catch (e) { print(e); } finally { break; // Noncompliant } } - return 1.0; + return 0; } } ---- @@ -65,14 +68,16 @@ class BadBreak { [source,dart] ---- class Ok { - double compliantMethod() { - var i = 5; - try { - i = 1 / 0; - } catch (e) { - log(e); + int nonCompliantMethod(int n) { + for (int i = 0; i < n; ++i) { + try { + functionThrowingException(i); + } catch (e) { + print(e); + return 1; + } } - return i; + return 0; } } ---- @@ -80,5 +85,6 @@ class Ok { == Resources +* https://dart.dev/tools/linter-rules/control_flow_in_finally[Dart Lint rule] * CWE - https://cwe.mitre.org/data/definitions/584[CWE-584 - Return Inside Finally Block] * https://wiki.sei.cmu.edu/confluence/x/BTdGBQ[CERT, ERR04-J.] - Do not complete abruptly from a finally block diff --git a/rules/S1161/dart/rule.adoc b/rules/S1161/dart/rule.adoc index 44abf5f7c0..f6cb991f24 100644 --- a/rules/S1161/dart/rule.adoc +++ b/rules/S1161/dart/rule.adoc @@ -28,3 +28,7 @@ class FirstChildClass extends ParentClass { bool doSomething(){/*...*/} // Compliant } ---- + +== Resources + +* https://dart.dev/tools/linter-rules/annotate_overrides[Dart Lint rule] \ No newline at end of file diff --git a/rules/S1185/dart/rule.adoc b/rules/S1185/dart/rule.adoc index 01b56e2623..41b4a32c40 100644 --- a/rules/S1185/dart/rule.adoc +++ b/rules/S1185/dart/rule.adoc @@ -15,7 +15,7 @@ There are cases when it is justified because redeclaring the function allows som [source,dart,diff-id=1,diff-type=noncompliant] ---- -class A extends B { +class Child extends Parent { @override void foo() { super.foo(); @@ -27,10 +27,14 @@ class A extends B { [source,dart,diff-id=1,diff-type=compliant] ---- -class A extends B { +class Child extends Parent { @override void foo() { - doSomethingElse(); + bar(); } } ---- + +== Resources + +* https://dart.dev/tools/linter-rules/unnecessary_overrides[Dart Linter rule] diff --git a/rules/S1206/dart/rule.adoc b/rules/S1206/dart/rule.adoc index af85bbccfe..d75ea19f93 100644 --- a/rules/S1206/dart/rule.adoc +++ b/rules/S1206/dart/rule.adoc @@ -11,12 +11,12 @@ By overriding only one of the two methods with a non-trivial implementation, thi [source,dart] ---- -class Bad { +class A { final int value; - Bad(this.value); + A(this.value); @override - bool operator ==(Object other) => other is Bad && other.value == value; + bool operator ==(Object other) => other is A && other.value == value; } ---- @@ -24,18 +24,18 @@ class Bad { [source,dart] ---- -class Better { +class A { final int value; - Better(this.value); + A(this.value); @override - bool operator ==(Object other) => - other is Better && - other.runtimeType == runtimeType && - other.value == value; + bool operator ==(Object other) => other is A && other.value == value; @override int get hashCode => value.hashCode; } ---- +== Resources + +* https://dart.dev/tools/linter-rules/unnecessary_overrides[Dart Lint rule] diff --git a/rules/S121/dart/rule.adoc b/rules/S121/dart/rule.adoc index 3c988af182..5c9bef7177 100644 --- a/rules/S121/dart/rule.adoc +++ b/rules/S121/dart/rule.adoc @@ -1 +1,5 @@ include::../rule.adoc[] + +== Resources + +* https://dart.dev/tools/linter-rules/curly_braces_in_flow_control_structures[Dart Lint rule] diff --git a/rules/S1481/dart/rule.adoc b/rules/S1481/dart/rule.adoc index 89014196b3..f20d20cff1 100644 --- a/rules/S1481/dart/rule.adoc +++ b/rules/S1481/dart/rule.adoc @@ -22,3 +22,7 @@ int numberOfMinutes(int hours) { return hours * 60; } ---- + +== Resources + +* https://dart.dev/tools/linter-rules/curly_braces_in_flow_control_structures[Dart compiler diagnostic] diff --git a/rules/S1874/dart/rule.adoc b/rules/S1874/dart/rule.adoc index 6f8d19fe28..c6bc99d5c2 100644 --- a/rules/S1874/dart/rule.adoc +++ b/rules/S1874/dart/rule.adoc @@ -17,3 +17,5 @@ oldFunction() // Noncompliant: "oldFunction is deprecated" ---- include::../see.adoc[] + +* https://dart.dev/tools/linter-rules/curly_braces_in_flow_control_structures[Dart Compiler diagnostic] diff --git a/rules/S6531/dart/rule.adoc b/rules/S6531/dart/rule.adoc index 61969d398e..2704c0c2e3 100644 --- a/rules/S6531/dart/rule.adoc +++ b/rules/S6531/dart/rule.adoc @@ -32,7 +32,7 @@ an unexpected compile-time type. ---- void types(num n) { if (n is int) { - (n as int).isEven; // Noncompliant: n is already known to be of type int + (n as int).isOdd; // Noncompliant: n is already known to be of type int } } ---- @@ -48,7 +48,7 @@ bool f(T a) => a is Object?; // Noncompliant: the type check is always true ---- void types(num n) { if (n is int) { - n.isEven; + n.isOdd; } } ---- @@ -63,6 +63,9 @@ bool f(T a) => a is Object; === Documentation * https://dart.dev/language/type-system[Dart type system] +* https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=unnecessary_cast#unnecessary_cast[Unnecessary Cast compiler diagnostic] +* https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=unnecessary_cast#unnecessary_type_check[Unnecessary Type Check compiler diagnostic] + === Articles & blog posts diff --git a/rules/S6535/dart/metadata.json b/rules/S6535/dart/metadata.json index 9b623610df..0db3279e44 100644 --- a/rules/S6535/dart/metadata.json +++ b/rules/S6535/dart/metadata.json @@ -1,3 +1,3 @@ { - "quickfix": "covered" + } diff --git a/rules/S6535/dart/rule.adoc b/rules/S6535/dart/rule.adoc index fb26ede141..884e74f1ed 100644 --- a/rules/S6535/dart/rule.adoc +++ b/rules/S6535/dart/rule.adoc @@ -29,3 +29,8 @@ const hello = 'Hello, world!'; const string1 = 'this string contains 2 "double quotes"'; const string2 = "this string contains 2 'single quotes'"; ---- + + +== Resources + +* https://dart.dev/tools/linter-rules/unnecessary_string_escapes[Dart Lint rule]