diff --git a/rules/S3498/javascript/metadata.json b/rules/S3498/javascript/metadata.json index 471a7d5d36..eb3053cbbc 100644 --- a/rules/S3498/javascript/metadata.json +++ b/rules/S3498/javascript/metadata.json @@ -30,4 +30,4 @@ "js", "ts" ] -} \ No newline at end of file +} diff --git a/rules/S3498/metadata.json b/rules/S3498/metadata.json index 2c63c08510..4efc7d3772 100644 --- a/rules/S3498/metadata.json +++ b/rules/S3498/metadata.json @@ -1,2 +1,35 @@ { + "title": "Object literal shorthand syntax should be used", + "type": "CODE_SMELL", + "code": { + "impacts": { + "MAINTAINABILITY": "LOW" + }, + "attribute": "CONVENTIONAL" + }, + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1min" + }, + "tags": [ + "convention", + "es2015" + ], + "extra": { + "replacementRules": [ + + ], + "legacyKeys": [ + + ] + }, + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-3498", + "sqKey": "S3498", + "scope": "Main", + "defaultQualityProfiles": [ + + ], + "quickfix": "covered" } diff --git a/rules/S3498/rust/metadata.json b/rules/S3498/rust/metadata.json new file mode 100644 index 0000000000..936079f5de --- /dev/null +++ b/rules/S3498/rust/metadata.json @@ -0,0 +1,9 @@ +{ + "title": "Field init shorthand should be used", + "tags": [ + "clippy" + ], + "defaultQualityProfiles": [ + "Sonar way" + ] +} diff --git a/rules/S3498/rust/rule.adoc b/rules/S3498/rust/rule.adoc new file mode 100644 index 0000000000..936a0a6f6c --- /dev/null +++ b/rules/S3498/rust/rule.adoc @@ -0,0 +1,40 @@ +== Why is this an issue? + +In Rust, field init shorthand syntax is a more concise way to define fields in structs. It was introduced to make struct initialization more readable and expressive. + +In the shorthand syntax, if a variable exists in the scope with the same name as the struct field you're defining, you can omit the field name and just write the variable name. The compiler will automatically understand that the field and the variable are linked. + +Using field init shorthand syntax can make your code cleaner and easier to read. It can also reduce the chance of making errors, as you don't have to repeat yourself by writing the variable name twice. + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +let a = 1; + +struct MyStruct { + a: i32, +} + +let my_struct = MyStruct { + a: a, // Noncompliant +}; +---- + +You can omit the field name and the colon if it is the same as the local variable name. + +[source,rust,diff-id=1,diff-type=compliant] +---- +let a = 1; + +struct MyStruct { + a: i32, +} + +let my_struct = MyStruct { + a, +}; +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names