Create rule S7051: Types should not be used as parameter names (avoid_types_as_parameter_names) (#4204)
Co-authored-by: antonioaversa <antonioaversa@users.noreply.github.com>
This commit is contained in:
parent
952c1cab7b
commit
db883d16c1
23
rules/S7051/dart/metadata.json
Normal file
23
rules/S7051/dart/metadata.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"title": "Types should not be used as parameter names",
|
||||
"type": "CODE_SMELL",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "3min"
|
||||
},
|
||||
"tags": [
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7051",
|
||||
"sqKey": "S7051",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "HIGH"
|
||||
},
|
||||
"attribute": "CLEAR"
|
||||
}
|
||||
}
|
78
rules/S7051/dart/rule.adoc
Normal file
78
rules/S7051/dart/rule.adoc
Normal file
@ -0,0 +1,78 @@
|
||||
Type names shouldn't be used parameter names of functions and methods.
|
||||
|
||||
== Why is this an issue?
|
||||
|
||||
Dart allows to use type names as parameter names of functions and methods.
|
||||
|
||||
This can be misleading, as the developer may think that the parameter is of the type corresponding to the specified name, but that is not the case.
|
||||
|
||||
A scenario leading to this issue is, for example, when the developer forgets to give a name to the parameter, or assumes that omitting the name and only specifying the type would be enough to make the parameter of that type (as in C++). That is not the case, as the parameter would be of type `dynamic`.
|
||||
|
||||
[source,dart]
|
||||
----
|
||||
void foo(int) { // int is a dynamic parameter, not an integer one
|
||||
print(int);
|
||||
}
|
||||
----
|
||||
|
||||
Dart also allows to use a type name both as type and as a name in the same declaration, which can be additional source of confusion:
|
||||
|
||||
[source,dart]
|
||||
----
|
||||
void foo(int int) { // the type may later change, but the name would remain int
|
||||
print(int);
|
||||
}
|
||||
----
|
||||
|
||||
== How to fix it
|
||||
|
||||
Give the parameter a different name, describing its purpose.
|
||||
|
||||
Make the type of the parameter explicit, if the parameter was not meant to be `dynamic`.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,dart,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
void foo(int) {
|
||||
print(int);
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,dart,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
void foo(String text) {
|
||||
print(text);
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
* Dart Docs - https://dart.dev/tools/linter-rules/avoid_types_as_parameter_names[Dart Linter rule - avoid_types_as_parameter_names]
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
=== Message
|
||||
|
||||
* The parameter name '<parameter name>' matches a visible type name.
|
||||
|
||||
=== Highlighting
|
||||
|
||||
The identifier of the parameter name, in the parameter list of a function or method declaration: e.g. `int` in `void foo(int) ...`.
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
endif::env-github,rspecator-view[]
|
||||
|
2
rules/S7051/metadata.json
Normal file
2
rules/S7051/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user