Skip to content

Commit

Permalink
Create rule S7101: "new" keyword shouldn't be used
Browse files Browse the repository at this point in the history
Co-authored-by: leveretka <[email protected]>
  • Loading branch information
github-actions[bot] and leveretka authored Sep 27, 2024
1 parent 7a02f98 commit 9ebf8f0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rules/S7101/dart/metadata.json
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"
}
}
56 changes: 56 additions & 0 deletions rules/S7101/dart/rule.adoc
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[]

2 changes: 2 additions & 0 deletions rules/S7101/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 9ebf8f0

Please sign in to comment.