diff --git a/rules/S7101/dart/metadata.json b/rules/S7101/dart/metadata.json new file mode 100644 index 00000000000..ba3c13bd348 --- /dev/null +++ b/rules/S7101/dart/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "\"new\" keyword shouldn't be used", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "2min" + }, + "tags": [ + ], + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-7101", + "sqKey": "S7101", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "LOW" + }, + "attribute": "CLEAR" + } +} diff --git a/rules/S7101/dart/rule.adoc b/rules/S7101/dart/rule.adoc new file mode 100644 index 00000000000..cc55dc74e47 --- /dev/null +++ b/rules/S7101/dart/rule.adoc @@ -0,0 +1,56 @@ +== Why is this an issue? + +In Dart language there's a `new` keyword. It is used with class constructors to create new instances. Since Dart 2.0 the `new` keyword is optional and can be omitted. As this keyword doesn't bring any additional value, it is recommended to avoid using it. + +By removing unnecessary `new`, you'll reduce the cognitive load and make code more readable and concise. Another effect of such change is that it makes easier the switch to const constructors, since they can't be invoked with the `new` keyword. + +== How to fix it + +Just remove unnecessary `new` keyword. + +=== Code examples + +==== Noncompliant code example + +[source,dart,diff-id=1,diff-type=noncompliant] +---- +void f() { + Person john = new Person("John", 45); // Noncompliant + Person john2 = new Person.copyAnotherPerson(john); // Noncompliant +} +---- + +==== Compliant solution + +[source,dart,diff-id=1,diff-type=compliant] +---- +void f() { + Person john = Person("John", 45); // Noncompliant + Person john2 = Person.copyAnotherPerson(john); // Noncompliant +} +---- + +== Resources + +=== Documentation + +* Dart Docs - https://dart.dev/tools/linter-rules/unnecessary_new[Linter rule - unnecessary_new] +* Dart Docs - https://dart.dev/language/classes#using-constructors[Language - using constructor] + + +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +=== Message + +Unnecessary 'new' keyword. + +=== Highlighting + +The `new` keyword + +endif::env-github,rspecator-view[] + diff --git a/rules/S7101/metadata.json b/rules/S7101/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S7101/metadata.json @@ -0,0 +1,2 @@ +{ +}