Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/mq-hir/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Source {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SourceInfo {
pub source_id: Option<SourceId>,
pub text_range: Option<mq_lang::Range>,
Expand Down
13 changes: 12 additions & 1 deletion crates/mq-lint/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum RuleId {
InfiniteLoop,
DeprecatedFunctionCall,
DuplicateMatchArm,
DuplicateImport,
ShadowVariable,
MissingElseInExpr,
AlwaysTrueCondition,
Expand All @@ -44,14 +45,15 @@ pub enum RuleId {

impl RuleId {
/// All known rule IDs.
pub const ALL: &'static [RuleId; 28] = &[
pub const ALL: &'static [RuleId; 29] = &[
RuleId::UnusedVariable,
RuleId::UnusedFunction,
RuleId::UnusedImport,
RuleId::UnreachableCode,
RuleId::InfiniteLoop,
RuleId::DeprecatedFunctionCall,
RuleId::DuplicateMatchArm,
RuleId::DuplicateImport,
RuleId::ShadowVariable,
RuleId::MissingElseInExpr,
RuleId::AlwaysTrueCondition,
Expand Down Expand Up @@ -84,6 +86,7 @@ impl RuleId {
RuleId::UnreachableCode => "unreachable_code",
RuleId::InfiniteLoop => "infinite_loop",
RuleId::DuplicateMatchArm => "duplicate_match_arm",
RuleId::DuplicateImport => "duplicate_import",
RuleId::DeprecatedFunctionCall => "deprecated_function_call",
RuleId::ShadowVariable => "shadow_variable",
RuleId::MissingElseInExpr => "missing_else_in_expr",
Expand Down Expand Up @@ -148,6 +151,9 @@ pub enum LintMessage {
DuplicateMatchArm {
pattern: String,
},
DuplicateImport {
name: String,
},
DeprecatedFunctionCall {
name: String,
},
Expand Down Expand Up @@ -227,6 +233,7 @@ impl LintMessage {
LintMessage::UnreachableCode { .. } => RuleId::UnreachableCode,
LintMessage::InfiniteLoop => RuleId::InfiniteLoop,
LintMessage::DuplicateMatchArm { .. } => RuleId::DuplicateMatchArm,
LintMessage::DuplicateImport { .. } => RuleId::DuplicateImport,
LintMessage::DeprecatedFunctionCall { .. } => RuleId::DeprecatedFunctionCall,
LintMessage::ShadowVariable { .. } => RuleId::ShadowVariable,
LintMessage::MissingElseInExpr => RuleId::MissingElseInExpr,
Expand Down Expand Up @@ -268,6 +275,9 @@ impl LintMessage {
LintMessage::DuplicateMatchArm { .. } => {
Some("remove or merge this arm with the earlier identical pattern".to_string())
}
LintMessage::DuplicateImport { .. } => {
Some("remove or merge this import with the earlier identical pattern".to_string())
}
LintMessage::DeprecatedFunctionCall { name } => Some(format!(
"deprecated function `{name}`; consider using an alternative or removing the call"
)),
Expand Down Expand Up @@ -351,6 +361,7 @@ impl fmt::Display for LintMessage {
LintMessage::UnreachableCode { keyword } => write!(f, "unreachable code after `{keyword}`"),
LintMessage::InfiniteLoop => write!(f, "loop without `break` may run forever"),
LintMessage::DuplicateMatchArm { pattern } => write!(f, "duplicate match arm pattern `{pattern}`"),
LintMessage::DuplicateImport { name } => write!(f, "duplicate import of module `{name}`"),
LintMessage::DeprecatedFunctionCall { name } => {
write!(f, "call to deprecated function `{name}`")
}
Expand Down
2 changes: 2 additions & 0 deletions crates/mq-lint/src/rules/correctness.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod always_true_condition;
pub mod deprecated_function_call;
pub mod duplicate_import;
pub mod duplicate_match_arm;
pub mod infinite_loop;
pub mod missing_else_in_expr;
Expand All @@ -20,6 +21,7 @@ pub fn all() -> Vec<Box<dyn LintRule>> {
Box::new(infinite_loop::InfiniteLoop),
Box::new(deprecated_function_call::DeprecatedFunctionCall),
Box::new(duplicate_match_arm::DuplicateMatchArm),
Box::new(duplicate_import::DuplicateImport),
Box::new(shadow_variable::ShadowVariable),
Box::new(missing_else_in_expr::MissingElseInExpr),
Box::new(always_true_condition::AlwaysTrueCondition),
Expand Down
81 changes: 81 additions & 0 deletions crates/mq-lint/src/rules/correctness/duplicate_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{Diagnostic, LintContext, LintMessage, LintRule, RuleId, Severity};
use rustc_hash::FxHashMap;

pub struct DuplicateImport;

impl LintRule for DuplicateImport {
fn id(&self) -> RuleId {
RuleId::DuplicateImport
}

fn severity(&self) -> Severity {
Severity::Error
}

fn check(&self, ctx: &LintContext<'_>) -> Vec<Diagnostic> {
let import_symbols = ctx
.all_symbols()
.filter_map(|(_, s)| if s.is_import() { Some(s) } else { None })
.collect::<Vec<_>>();
let mut counts = FxHashMap::default();

for s in &import_symbols {
match &s.value {
Some(name) => {
counts.entry(name).or_insert((s, 0)).1 += 1;
}
None => {
// If the import has no name, we can skip it
continue;
}
}
}

counts
.into_iter()
.filter(|(_, (_, count))| *count > 1)
.map(|(name, (s, _))| {
let mut d = Diagnostic::new(LintMessage::DuplicateImport { name: name.to_string() }, self.severity());

if let Some(range) = s.source.text_range {
d = d.with_range(range);
}
d
})
.collect()
}
}

#[cfg(test)]
mod tests {
use mq_hir::Hir;

use super::*;
use crate::{LintConfig, LintContext};

fn check(code: &str) -> Vec<Diagnostic> {
let mut hir = Hir::default();
let (source_id, _) = hir.add_code(None, code);
let config = LintConfig::default();
let ctx = LintContext::new(&hir, source_id, &config);
DuplicateImport.check(&ctx)
}

#[test]
fn detects_duplicate_import() {
let diags = check("import \"a\" | import \"a\" | a");
assert_eq!(diags.len(), 1);
}

#[test]
fn no_duplicate_import_with_other_module() {
let diags = check("import \"a\" | import \"b\" | a");
assert_eq!(diags.len(), 0);
}

#[test]
fn no_duplicate_import() {
let diags = check("import \"a\" | a");
assert_eq!(diags.len(), 0);
}
}