Skip to content

Commit

Permalink
deduplicate lints
Browse files Browse the repository at this point in the history
  • Loading branch information
orpuente-MS committed Jan 27, 2025
1 parent 6ddfee3 commit 23e2736
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/qsc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ qsc_hir = { path = "../qsc_hir" }
qsc_data_structures = { path = "../qsc_data_structures" }
qsc_frontend = { path = "../qsc_frontend" }
qsc_doc_gen = { path = "../qsc_doc_gen" }
rustc-hash = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }

Expand Down
23 changes: 22 additions & 1 deletion compiler/qsc_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ pub fn run_lints(
let mut lints = Vec::new();
lints.append(&mut ast_lints);
lints.append(&mut hir_lints);
remove_duplicates(&mut lints);
lints
}

pub(crate) fn remove_duplicates<T: Eq + std::hash::Hash + Clone>(vec: &mut Vec<T>) {
let mut seen = rustc_hash::FxHashSet::default();
vec.retain(|x| seen.insert(x.clone()));
}

#[derive(Clone, Copy)]
pub(crate) struct Compilation<'a> {
pub package_store: &'a PackageStore,
Expand Down Expand Up @@ -117,6 +123,21 @@ pub struct Lint {
pub code_action_edits: Vec<(String, Span)>,
}

impl std::hash::Hash for Lint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.span.hash(state);
self.kind.hash(state);
}
}

impl std::cmp::PartialEq for Lint {
fn eq(&self, other: &Self) -> bool {
self.span == other.span && self.kind == other.kind
}
}

impl std::cmp::Eq for Lint {}

impl std::fmt::Display for Lint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
Expand Down Expand Up @@ -188,7 +209,7 @@ pub struct LintConfig {
}

/// Represents a lint name.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum LintKind {
/// AST lint name.
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_linter/src/linter/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ macro_rules! declare_ast_lints {
use serde::{Deserialize, Serialize};

/// An enum listing all existing AST lints.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum AstLint {
$(
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_linter/src/linter/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ macro_rules! declare_hir_lints {
use serde::{Deserialize, Serialize};

/// An enum listing all existing HIR lints.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum HirLint {
$(
Expand Down
48 changes: 25 additions & 23 deletions compiler/qsc_linter/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::{
linter::{ast::run_ast_lints, hir::run_hir_lints, Compilation},
Lint, LintConfig, LintLevel,
};
use crate::{run_lints, Lint, LintLevel};
use expect_test::{expect, Expect};
use indoc::indoc;
use qsc_data_structures::{
language_features::LanguageFeatures, span::Span, target::TargetCapabilityFlags,
};
use qsc_frontend::compile::{self, CompileUnit, PackageStore, SourceMap};
use qsc_frontend::compile::{self, PackageStore, SourceMap};
use qsc_hir::hir::CallableKind;
use qsc_passes::PackageType;

#[test]
fn check_that_hir_lints_are_deduplicated_in_operations_with_multiple_specializations() {
check(
"
operation Main() : Unit {}
operation LintProblem() : Unit is Adj + Ctl {
use q = Qubit();
0.0 == 0.0;
}",
&expect![[r#"
[
SrcLint {
source: "0.0 == 0.0",
level: Warn,
message: "strict comparison of doubles",
help: "consider comparing them with some margin of error",
code_action_edits: [],
},
]
"#]],
);
}

#[test]
fn daisy_chain_lint() {
check(
Expand Down Expand Up @@ -805,21 +825,3 @@ impl SrcLint {
}
}
}

fn run_lints(
package_store: &PackageStore,
compile_unit: &CompileUnit,
config: Option<&[LintConfig]>,
) -> Vec<Lint> {
let compilation = Compilation {
package_store,
compile_unit,
};

let mut ast_lints = run_ast_lints(&compile_unit.ast.package, config, compilation);
let mut hir_lints = run_hir_lints(&compile_unit.package, config, compilation);
let mut lints = Vec::new();
lints.append(&mut ast_lints);
lints.append(&mut hir_lints);
lints
}

0 comments on commit 23e2736

Please sign in to comment.