Skip to content

Commit

Permalink
Show dead code with reduced opacity (FuelLabs#3800)
Browse files Browse the repository at this point in the history
Closes FuelLabs#3747

I realized that I missed a couple declaration types in
FuelLabs#3769, so I updated those to use
the name spans as well.
- const declarations
- variable declarations


Before:

<img width="494" alt="image"
src="https://user-images.githubusercontent.com/47993817/213106817-0b093ec0-599e-4200-901d-bc64a0388a26.png">

After:

<img width="569" alt="image"
src="https://user-images.githubusercontent.com/47993817/213063161-842bdc27-2bdb-40e7-aad0-93c7413af318.png">
  • Loading branch information
sdankel authored Jan 18, 2023
1 parent a97c97f commit 0c99c8a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 22 deletions.
33 changes: 33 additions & 0 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,39 @@ fn construct_dead_code_warning_from_node(
warning_content: Warning::DeadTrait,
}
}
ty::TyAstNode {
content:
ty::TyAstNodeContent::Declaration(ty::TyDeclaration::ConstantDeclaration(decl_id)),
span,
} => {
let warning_span = match decl_engine.get_constant(decl_id.clone(), span) {
Ok(ty::TyConstantDeclaration { name, .. }) => name.span(),
Err(_) => span.clone(),
};
CompileWarning {
span: warning_span,
warning_content: Warning::DeadDeclaration,
}
}
ty::TyAstNode {
content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::VariableDeclaration(decl)),
span,
} => {
// In rare cases, variable declaration spans don't have a path, so we need to check for that
if decl.name.span().path().is_some() {
CompileWarning {
span: decl.name.span(),
warning_content: Warning::DeadDeclaration,
}
} else if span.path().is_some() {
CompileWarning {
span: span.clone(),
warning_content: Warning::DeadDeclaration,
}
} else {
return None;
}
}
ty::TyAstNode {
content: ty::TyAstNodeContent::Declaration(ty::TyDeclaration::ImplTrait(decl_id)),
span,
Expand Down
24 changes: 22 additions & 2 deletions sway-lsp/src/capabilities/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Position, Range};
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, Position, Range};

use sway_error::error::CompileError;
use sway_error::warning::CompileWarning;
use sway_error::{error::CompileError, warning::Warning};
use sway_types::{LineCol, Spanned};

#[derive(Debug)]
Expand All @@ -24,6 +24,7 @@ fn get_warning_diagnostics(warnings: &[CompileWarning]) -> Vec<Diagnostic> {
range: get_range(warning.span().line_col()),
severity: Some(DiagnosticSeverity::WARNING),
message: warning.to_friendly_warning_string(),
tags: get_warning_diagnostic_tags(&warning.warning_content),
..Default::default()
}))
}
Expand All @@ -41,3 +42,22 @@ fn get_range((start, end): (LineCol, LineCol)) -> Range {
let end = pos(end);
Range { start, end }
}

fn get_warning_diagnostic_tags(warning: &Warning) -> Option<Vec<DiagnosticTag>> {
match warning {
Warning::StructFieldNeverRead
| Warning::DeadDeclaration
| Warning::DeadEnumDeclaration
| Warning::DeadEnumVariant { .. }
| Warning::DeadFunctionDeclaration
| Warning::DeadMethod
| Warning::DeadStorageDeclaration
| Warning::DeadStorageDeclarationForFunction { .. }
| Warning::DeadStructDeclaration
| Warning::DeadTrait
| Warning::MatchExpressionUnreachableArm
| Warning::UnreachableCode
| Warning::UnusedReturnValue { .. } => Some(vec![DiagnosticTag::UNNECESSARY]),
_ => None,
}
}
77 changes: 58 additions & 19 deletions sway-lsp/test/fixtures/diagnostics/dead_code/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"line": 31
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "Enum variant A is never constructed.",
Expand All @@ -29,7 +32,10 @@
"line": 25
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "Enum variant B is never constructed.",
Expand All @@ -43,35 +49,44 @@
"line": 26
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This declaration is never used.",
"range": {
"end": {
"character": 24,
"character": 18,
"line": 13
},
"start": {
"character": 0,
"character": 6,
"line": 13
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This declaration is never used.",
"range": {
"end": {
"character": 38,
"character": 24,
"line": 14
},
"start": {
"character": 0,
"character": 6,
"line": 14
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This struct is never used.",
Expand All @@ -85,7 +100,10 @@
"line": 15
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This struct field is never accessed.",
Expand All @@ -99,7 +117,10 @@
"line": 16
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This struct field is never accessed.",
Expand All @@ -113,7 +134,10 @@
"line": 17
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This trait is never implemented.",
Expand All @@ -127,7 +151,10 @@
"line": 20
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This enum is never used.",
Expand All @@ -141,7 +168,10 @@
"line": 24
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This function is never called.",
Expand All @@ -155,21 +185,27 @@
"line": 34
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This declaration is never used.",
"range": {
"end": {
"character": 23,
"character": 18,
"line": 35
},
"start": {
"character": 4,
"character": 8,
"line": 35
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
},
{
"message": "This function is never called.",
Expand All @@ -183,7 +219,10 @@
"line": 42
}
},
"severity": 2
"severity": 2,
"tags": [
1
]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion sway-types/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub trait Spanned {
fn span(&self) -> Span;
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct LineCol {
pub line: usize,
pub col: usize,
Expand Down

0 comments on commit 0c99c8a

Please sign in to comment.