-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S7101: "new" keyword shouldn't be used
Co-authored-by: leveretka <[email protected]>
- Loading branch information
1 parent
7a02f98
commit 9ebf8f0
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |