SONARJAVA-5163 Create rule S7198: Path.of should be preferred to Paths.get (#4670)
This commit is contained in:
parent
1dc40b1a64
commit
afc4f2638a
26
rules/S7198/java/metadata.json
Normal file
26
rules/S7198/java/metadata.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"title": "\"Path.of\" should be preferred to \"Paths.get\"",
|
||||
"type": "CODE_SMELL",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"java11"
|
||||
],
|
||||
"defaultSeverity": "Minor",
|
||||
"ruleSpecification": "RSPEC-7198",
|
||||
"sqKey": "S7198",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "targeted",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "MEDIUM",
|
||||
"RELIABILITY": "LOW",
|
||||
"SECURITY": "LOW"
|
||||
},
|
||||
"attribute": "CONVENTIONAL"
|
||||
}
|
||||
}
|
29
rules/S7198/java/rule.adoc
Normal file
29
rules/S7198/java/rule.adoc
Normal file
@ -0,0 +1,29 @@
|
||||
== Why is this an issue?
|
||||
|
||||
`Path.of()` provides a modern and consistent approach to creating Path instances, aligning with factory methods such as `List.of()` and `Set.of()`.
|
||||
Additionally, `Paths.get()` may be deprecated in future versions of Java.
|
||||
|
||||
== How to fix it
|
||||
|
||||
Replace `Paths.get()` with `Path.of()`.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
|
||||
[source,java,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
Path path = Paths.get("my/file.txt"); // non compliant
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,java,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
Path path = Path.of("my/file.txt"); // compliant
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
- Java Documentation - https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Paths.html[Paths]
|
2
rules/S7198/metadata.json
Normal file
2
rules/S7198/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user