Skip to content

Commit

Permalink
Ignore identifiers starting with _ from liveness check
Browse files Browse the repository at this point in the history
Partially fixes #55
Closes #109
  • Loading branch information
oxalica committed Oct 9, 2023
1 parent dbf49b3 commit 61caccd
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/ide/src/def/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
//! - Unused `with` expressions.
//! - Unnecessary `rec` attrsets.
//! - Unused parameters of a package.
//!
//! Notes:
//! - All identifiers starting with `_` are skipped from warnings. This also includes Nix internals
//! starting with `__`, eg. `__findFile` <https://github.com/oxalica/nil/pull/109>.
use super::{BindingValue, DefDatabase, Expr, ExprId, NameId, ResolveResult};
use crate::{Diagnostic, DiagnosticKind, FileId, ModuleKind};
use la_arena::ArenaMap;
Expand All @@ -25,6 +29,10 @@ use std::sync::Arc;
use syntax::ast::{self, AstNode};
use syntax::TextRange;

fn should_ignore(name: &str) -> bool {
name.starts_with('_')
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct LivenessCheckResult {
names: Box<[NameId]>,
Expand Down Expand Up @@ -207,6 +215,8 @@ pub(crate) fn liveness_check_query(
}
}

unused_defs.retain(|name| !should_ignore(&module[*name].text));

Arc::new(LivenessCheckResult {
names: unused_defs.into(),
withs: unused_withs.into(),
Expand Down Expand Up @@ -353,4 +363,10 @@ mod tests {
",
);
}

#[test]
fn underscore_names() {
check("x: _y: x");
check("let __findFile = 42; in <nixpkgs>");
}
}

0 comments on commit 61caccd

Please sign in to comment.