From 6ca7dd2cc65204873fa72b63395b4626a63dd8ce Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:37:31 +0100 Subject: [PATCH] Create rule S2208: Wildcard imports should not be used (#4743) * Add rust to rule S2208 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource Co-authored-by: yassin-kammoun-sonarsource --- rules/S2208/rust/metadata.json | 9 +++++ rules/S2208/rust/rule.adoc | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 rules/S2208/rust/metadata.json create mode 100644 rules/S2208/rust/rule.adoc diff --git a/rules/S2208/rust/metadata.json b/rules/S2208/rust/metadata.json new file mode 100644 index 0000000000..bf8cbaf118 --- /dev/null +++ b/rules/S2208/rust/metadata.json @@ -0,0 +1,9 @@ +{ + "tags": [ + "pitfall", + "clippy" + ], + "defaultQualityProfiles": [ + "Sonar way" + ] +} diff --git a/rules/S2208/rust/rule.adoc b/rules/S2208/rust/rule.adoc new file mode 100644 index 0000000000..911f136490 --- /dev/null +++ b/rules/S2208/rust/rule.adoc @@ -0,0 +1,62 @@ +== Why is this an issue? + +Importing every public name from a module using a wildcard (``++use _::*++``) is a bad idea because: + +* It could lead to conflicts between names defined locally and the ones imported. +* It reduces code readability as developers will have a hard time knowing where names come from. +* It clutters the local namespace, which makes debugging more difficult. + +Remember that imported names can change when you update your dependencies. A wildcard import that works today might be broken tomorrow. + +=== Exceptions + +No issue will be raised for modules that their name contains ``++prelude ++``. Many crates, including the standard library, provide modules named ``++prelude ++`` that are specifically designed for wildcard import. + +No issue will be raised in test modules. This is defined as any module with ``++test++`` in the name. + +== How to fix it + +There are two ways to avoid a wildcard import: + +* Replace it with `use mymodule;` and access module members as `mymodule::myfunction`. If the module name is too long, alias it to a shorter name. Example: `use std::collections as col;` +* List every imported name. If necessary, import statements can be split on multiple lines using parentheses (preferred solution) or backslashes. + +=== Code examples + +==== Noncompliant code example + +[source,rust] +---- +use std::collections::*; // Noncompliant +fn main() { + let mut map = HashMap::new(); + map.insert(1, 2); +} +---- + +==== Compliant solution + +[source,rust] +---- +use std::collections::HashMap; +fn main() { + let mut map = HashMap::new(); + map.insert(1, 2); +} +---- +Or + +[source,rust] +---- +use std::collections::HashMap as HM; +fn main() { + let mut map = HM::new(); + map.insert(1, 2); +} +---- + +== Resources + +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_imports