Skip to content

Commit 9ceee64

Browse files
authored
Unused analyzers: disable in VS when file has errors (#19892)
1 parent 9983e82 commit 9ceee64

7 files changed

Lines changed: 25 additions & 9 deletions

File tree

docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888

8989
* Added warning FS3884 when a function or delegate value is used as an interpolated string argument. ([PR #19289](https://github.com/dotnet/fsharp/pull/19289))
9090
* Symbols: add ObsoleteDiagnosticInfo ([PR #19359](https://github.com/dotnet/fsharp/pull/19359))
91+
* FCS: add FSharpCheckFileResults.HasErrors ([PR #19892](https://github.com/dotnet/fsharp/pull/19892))
9192
* Add `#version;;` directive to F# Interactive to display version and environment information. ([Issue #13307](https://github.com/dotnet/fsharp/issues/13307), [PR #19332](https://github.com/dotnet/fsharp/pull/19332))
9293
* Debug: rework for expressions stepping ([PR #19894](https://github.com/dotnet/fsharp/pull/19894))
9394

docs/release-notes/.VisualStudio/18.vNext.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
### Changed
1515

1616
* Rename "inline hints" to "inlay hints" in VS options for consistency with industry terminology. ([PR #19318](https://github.com/dotnet/fsharp/pull/19318))
17+
* Unused analyzers: disable in VS when file has errors ([PR #19892](https://github.com/dotnet/fsharp/pull/19892))

src/Compiler/Service/FSharpCheckerResults.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3411,6 +3411,9 @@ type FSharpCheckFileResults
34113411

34123412
member _.Diagnostics = errors
34133413

3414+
member _.HasErrors =
3415+
errors |> Array.exists (fun d -> d.Severity = FSharpDiagnosticSeverity.Error)
3416+
34143417
member _.HasFullTypeCheckInfo = details.IsSome
34153418

34163419
member _.TryGetCurrentTcImports() =

src/Compiler/Service/FSharpCheckerResults.fsi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ type public FSharpCheckFileResults =
253253
/// The errors returned by parsing a source file.
254254
member Diagnostics: FSharpDiagnostic[]
255255

256+
member HasErrors: bool
257+
256258
/// Get a view of the contents of the assembly up to and including the file just checked
257259
member PartialAssemblySignature: FSharpAssemblySignature
258260

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.bsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,8 +2085,10 @@ FSharp.Compiler.CodeAnalysis.FSharpCheckFileAnswer: Int32 GetHashCode(System.Col
20852085
FSharp.Compiler.CodeAnalysis.FSharpCheckFileAnswer: Int32 Tag
20862086
FSharp.Compiler.CodeAnalysis.FSharpCheckFileAnswer: Int32 get_Tag()
20872087
FSharp.Compiler.CodeAnalysis.FSharpCheckFileAnswer: System.String ToString()
2088+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Boolean HasErrors
20882089
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Boolean HasFullTypeCheckInfo
20892090
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Boolean IsRelativeNameResolvableFromSymbol(FSharp.Compiler.Text.Position, Microsoft.FSharp.Collections.FSharpList`1[System.String], FSharp.Compiler.Symbols.FSharpSymbol)
2091+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Boolean get_HasErrors()
20902092
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Boolean get_HasFullTypeCheckInfo()
20912093
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: FSharp.Compiler.CodeAnalysis.FSharpProjectContext ProjectContext
20922094
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: FSharp.Compiler.CodeAnalysis.FSharpProjectContext get_ProjectContext()

vsintegration/src/FSharp.Editor/Diagnostics/UnusedDeclarationsAnalyzer.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ type internal UnusedDeclarationsAnalyzer [<ImportingConstructor>] () =
3333

3434
let! unusedRanges = UnusedDeclarations.getUnusedDeclarations (checkResults, (isScriptFile document.FilePath))
3535

36-
let! sourceText = document.GetTextAsync()
37-
38-
return
39-
unusedRanges
40-
|> Seq.map (fun m -> Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(m, sourceText, document.FilePath)))
41-
|> Seq.toImmutableArray
36+
if checkResults.HasErrors then
37+
return ImmutableArray.Empty
38+
else
39+
let! sourceText = document.GetTextAsync()
40+
41+
return
42+
unusedRanges
43+
|> Seq.map (fun m ->
44+
Diagnostic.Create(descriptor, RoslynHelpers.RangeToLocation(m, sourceText, document.FilePath)))
45+
|> Seq.toImmutableArray
4246
}
4347
|> CancellableTask.start cancellationToken

vsintegration/src/FSharp.Editor/Diagnostics/UnusedOpensDiagnosticAnalyzer.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ type internal UnusedOpensDiagnosticAnalyzer [<ImportingConstructor>] () =
2929

3030
let! _, checkResults = document.GetFSharpParseAndCheckResultsAsync(nameof UnusedOpensDiagnosticAnalyzer)
3131

32-
let! unusedOpens =
33-
UnusedOpens.getUnusedOpens (checkResults, (fun lineNumber -> sourceText.Lines[Line.toZ lineNumber].ToString()))
32+
if checkResults.HasErrors then
33+
return ValueNone
34+
else
35+
let! unusedOpens =
36+
UnusedOpens.getUnusedOpens (checkResults, (fun lineNumber -> sourceText.Lines[Line.toZ lineNumber].ToString()))
3437

35-
return (ValueSome unusedOpens)
38+
return (ValueSome unusedOpens)
3639
}
3740

3841
interface IFSharpUnusedOpensDiagnosticAnalyzer with

0 commit comments

Comments
 (0)