From 6748240e59d74b823097438c11f3807093139211 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Fri, 26 Apr 2024 13:49:28 +0800 Subject: [PATCH] libnixf: omit pedantic liveness warning on lambda arg (#464) --- libnixf/src/Sema/VariableLookup.cpp | 5 ++ .../nixd/test/diagnostic-liveness-formal.md | 62 +++++++++++++++++++ nixd/tools/nixd/test/diagnostic-liveness.md | 23 +------ 3 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 nixd/tools/nixd/test/diagnostic-liveness-formal.md diff --git a/libnixf/src/Sema/VariableLookup.cpp b/libnixf/src/Sema/VariableLookup.cpp index b56e1ac49..70040124a 100644 --- a/libnixf/src/Sema/VariableLookup.cpp +++ b/libnixf/src/Sema/VariableLookup.cpp @@ -43,6 +43,11 @@ bool EnvNode::isLive() const { void VariableLookupAnalysis::emitEnvLivenessWarning( const std::shared_ptr &NewEnv) { for (const auto &[Name, Def] : NewEnv->defs()) { + // If the definition comes from lambda arg, omit the diagnostic + // because there is no elegant way to "fix" this trivially & keep + // the lambda signature. + if (Def->source() == Definition::DS_LambdaArg) + continue; if (Def->uses().empty()) { Diagnostic &D = Diags.emplace_back(Diagnostic::DK_DefinitionNotUsed, Def->syntax()->range()); diff --git a/nixd/tools/nixd/test/diagnostic-liveness-formal.md b/nixd/tools/nixd/test/diagnostic-liveness-formal.md new file mode 100644 index 000000000..4fc39b44b --- /dev/null +++ b/nixd/tools/nixd/test/diagnostic-liveness-formal.md @@ -0,0 +1,62 @@ +# RUN: nixd --lit-test < %s | FileCheck %s + +<-- initialize(0) + +```json +{ + "jsonrpc":"2.0", + "id":0, + "method":"initialize", + "params":{ + "processId":123, + "rootPath":"", + "capabilities":{ + }, + "trace":"off" + } +} +``` + + +<-- textDocument/didOpen + +```json +{ + "jsonrpc":"2.0", + "method":"textDocument/didOpen", + "params":{ + "textDocument":{ + "uri":"file:///basic.nix", + "languageId":"nix", + "version":1, + "text":"{x, y}: x + 1" + } + } +} +``` + +``` + CHECK: "diagnostics": [ +CHECK-NEXT: { +CHECK-NEXT: "code": "sema-def-not-used", +CHECK-NEXT: "message": "definition `y` is not used", +CHECK-NEXT: "range": { +CHECK-NEXT: "end": { +CHECK-NEXT: "character": 5, +CHECK-NEXT: "line": 0 +CHECK-NEXT: }, +CHECK-NEXT: "start": { +CHECK-NEXT: "character": 4, +CHECK-NEXT: "line": 0 +CHECK-NEXT: } +CHECK-NEXT: }, +CHECK-NEXT: "relatedInformation": [], +CHECK-NEXT: "severity": 2, +CHECK-NEXT: "source": "nixf", +CHECK-NEXT: "tags": [ +CHECK-NEXT: 1 +CHECK-NEXT: ] +CHECK-NEXT: } +CHECK-NEXT: ], +CHECK-NEXT: "uri": "file:///basic.nix", +``` diff --git a/nixd/tools/nixd/test/diagnostic-liveness.md b/nixd/tools/nixd/test/diagnostic-liveness.md index 06fec9492..e8ec72c09 100644 --- a/nixd/tools/nixd/test/diagnostic-liveness.md +++ b/nixd/tools/nixd/test/diagnostic-liveness.md @@ -36,28 +36,7 @@ ``` ``` - CHECK: "diagnostics": [ -CHECK-NEXT: { -CHECK-NEXT: "code": "sema-def-not-used", -CHECK-NEXT: "message": "definition `y` is not used", -CHECK-NEXT: "range": { -CHECK-NEXT: "end": { -CHECK-NEXT: "character": 4, -CHECK-NEXT: "line": 0 -CHECK-NEXT: }, -CHECK-NEXT: "start": { -CHECK-NEXT: "character": 3, -CHECK-NEXT: "line": 0 -CHECK-NEXT: } -CHECK-NEXT: }, -CHECK-NEXT: "relatedInformation": [], -CHECK-NEXT: "severity": 2, -CHECK-NEXT: "source": "nixf", -CHECK-NEXT: "tags": [ -CHECK-NEXT: 1 -CHECK-NEXT: ] -CHECK-NEXT: } -CHECK-NEXT: ], + CHECK: "diagnostics": [], CHECK-NEXT: "uri": "file:///basic.nix", CHECK-NEXT: "version": 1 ```