Skip to content

Commit

Permalink
add experimental switch call linter to dtacheck
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkRTA committed Jan 11, 2024
1 parent 13ad008 commit e5c7c8e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions crates/dtacheck/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn lint_node(lints: &mut Vec<Box<dyn Lint>>, ast: &[Node], funcs: &Function) {

if !has_preprocessor_directive {
lint_fn_args(lints, array, node.span.clone(), funcs);
lint_switch_fallthrough(lints, array, node.span.clone());
}
}
_ => (),
Expand Down Expand Up @@ -211,3 +212,46 @@ fn lint_preprocs(lints: &mut Vec<Box<dyn Lint>>, tokens: &[Token]) {
lints.push(Box::new(PreProcLint::Unmatched(lint.0)));
}
}

// switch fallthough

struct SwitchFallthroughLint(Range<usize>, Range<usize>);

impl Lint for SwitchFallthroughLint {
fn to_codespan(&self, id: usize) -> Diagnostic<usize> {
Diagnostic::warning()
.with_message("missing fallthrough for switch")
.with_labels(vec![
Label::primary(id, self.0.clone()),
Label::secondary(id, self.1.clone())
.with_message("consider adding a fallthrough node here"),
])
}
}

fn lint_switch_fallthrough(
lints: &mut Vec<Box<dyn Lint>>,
stmt: &[Node],
span: Range<usize>,
) {
if stmt.is_empty() {
return;
}

let NodeKind::Symbol(ref sym) = stmt[0].kind else {
return;
};

if sym != "switch" {
return;
}

let Some(last_node) = stmt.last() else {
return;
};

if last_node.kind.is_array() {
let pos = span.end - 1;
lints.push(Box::new(SwitchFallthroughLint(span, pos..pos)))
}
}

0 comments on commit e5c7c8e

Please sign in to comment.