From 61caccdde0b3a6ed7ab17f817d3573acdc0c7ce5 Mon Sep 17 00:00:00 2001 From: oxalica Date: Mon, 9 Oct 2023 09:17:31 +0800 Subject: [PATCH] Ignore identifiers starting with `_` from liveness check Partially fixes #55 Closes #109 --- crates/ide/src/def/liveness.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/ide/src/def/liveness.rs b/crates/ide/src/def/liveness.rs index 9402e82..6ccc8f9 100644 --- a/crates/ide/src/def/liveness.rs +++ b/crates/ide/src/def/liveness.rs @@ -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` . use super::{BindingValue, DefDatabase, Expr, ExprId, NameId, ResolveResult}; use crate::{Diagnostic, DiagnosticKind, FileId, ModuleKind}; use la_arena::ArenaMap; @@ -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]>, @@ -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(), @@ -353,4 +363,10 @@ mod tests { ", ); } + + #[test] + fn underscore_names() { + check("x: _y: x"); + check("let __findFile = 42; in "); + } }