forked from G-Research/fsharp-analyzers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisposedBeforeAsyncRunAnalyzer.fs
More file actions
160 lines (139 loc) · 5.95 KB
/
Copy pathDisposedBeforeAsyncRunAnalyzer.fs
File metadata and controls
160 lines (139 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module GR.FSharp.Analyzers.DisposedBeforeAsyncRunAnalyzer
open System
open FSharp.Analyzers.Comments
open FSharp.Analyzers.SDK
open FSharp.Analyzers.SDK.ASTCollecting
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Symbols
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open FSharp.Compiler.Text
[<Literal>]
let Code = "GRA-DISPBEFOREASYNC-001"
let getType (checkFileResults : FSharpCheckFileResults) (sourceText : ISourceText) (synPat : SynPat) =
let lineText = sourceText.GetLineString (synPat.Range.EndLine - 1) // 0-based
match synPat with
| SynPat.LongIdent (longDotId = SynLongIdent (id = idents)) -> idents |> List.tryLast
| SynPat.Named (ident = SynIdent (ident = ident)) -> Some ident
| _ -> None
|> Option.bind (fun i ->
checkFileResults.GetSymbolUseAtLocation (synPat.Range.EndLine, synPat.Range.EndColumn, lineText, [ i.idText ])
|> Option.bind (fun symbolUse ->
match symbolUse.Symbol with
| :? FSharpMemberOrFunctionOrValue as mfv when mfv.IsFunction -> mfv.FullTypeSafe
| _ -> None
)
)
let asyncOrTask (t : FSharpType) =
t.GenericArguments
|> Seq.tryLast
|> Option.bind (fun t ->
if t.HasTypeDefinition then
match t.TypeDefinition.TryGetFullName () with
| Some fullName when fullName.StartsWith ("Microsoft.FSharp.Control.FSharpAsync", StringComparison.Ordinal) ->
Some "async"
| Some fullName when fullName.StartsWith ("System.Threading.Tasks.Task", StringComparison.Ordinal) ->
Some "task"
| _ -> None
else
None
)
let pathContainsAsyncOrTaskReturningFunc
(checkFileResults : FSharpCheckFileResults)
(sourceText : ISourceText)
(path : SyntaxVisitorPath)
=
path
|> List.tryPick (fun node ->
match node with
| SyntaxNode.SynBinding (SynBinding (headPat = headPat)) ->
getType checkFileResults sourceText headPat |> Option.bind asyncOrTask
| _ -> None
)
let pathContainsComputationExpr (path : SyntaxVisitorPath) =
path
|> List.exists (
function
| SyntaxNode.SynExpr (SynExpr.ComputationExpr _) -> true
| _ -> false
)
[<Literal>]
let SwitchOffComment = "disposed before returned workflow runs"
let collectUses (sourceText : ISourceText) (ast : ParsedInput) (checkFileResults : FSharpCheckFileResults) =
let comments =
match ast with
| ParsedInput.ImplFile parsedImplFileInput -> parsedImplFileInput.Trivia.CodeComments
| _ -> []
let uses = ResizeArray<range * string> ()
// Note: not tailrecursive
let rec hasAsyncOrTaskInBody (body : SynExpr) =
match body with
| SynExpr.App (funcExpr = SynExpr.Ident (ident = ident)) -> ident.idText = "async" || ident.idText = "task"
| SynExpr.LetOrUse (body = body) -> hasAsyncOrTaskInBody body
| SynExpr.Sequential (expr2 = expr2) -> hasAsyncOrTaskInBody expr2
| SynExpr.IfThenElse (thenExpr = thenExpr ; elseExpr = elseExpr) ->
hasAsyncOrTaskInBody thenExpr
|| elseExpr |> Option.map hasAsyncOrTaskInBody |> Option.defaultValue false
| SynExpr.TryFinally (tryExpr = tryExpr) -> hasAsyncOrTaskInBody tryExpr
| SynExpr.TryWith (tryExpr = tryExpr) -> hasAsyncOrTaskInBody tryExpr
| SynExpr.Match (clauses = clauses) ->
clauses
|> List.exists (fun (SynMatchClause (resultExpr = resultExpr)) -> hasAsyncOrTaskInBody resultExpr)
| _ -> false
let walker =
{ new SyntaxCollectorBase() with
override _.WalkExpr (path : SyntaxVisitorPath, synExpr : SynExpr) : unit =
if not (pathContainsComputationExpr path) then
match synExpr with
| SynExpr.LetOrUse (isUse = true ; bindings = [ binding ] ; body = body) ->
if
not (
isSwitchedOffPerComment
SwitchOffComment
comments
sourceText
binding.RangeOfBindingWithoutRhs
)
&& hasAsyncOrTaskInBody body
then
match pathContainsAsyncOrTaskReturningFunc checkFileResults sourceText path with
| Some ce -> uses.Add (binding.RangeOfBindingWithoutRhs, ce)
| _ -> ()
| _ -> ()
}
walkAst walker ast
uses.ToArray ()
let analyze (sourceText : ISourceText) ast (checkFileResults : FSharpCheckFileResults) =
let uses = collectUses sourceText ast checkFileResults
[
for useRange, ce in uses do
{
Type = "DisposedBeforeAsyncRun analyzer"
Message = $"Object is disposed before returned %s{ce} is run"
Code = Code
Severity = Severity.Warning
Range = useRange
Fixes = []
}
]
[<Literal>]
let Name = "DisposedBeforeAsyncRunAnalyzer"
[<Literal>]
let ShortDescription =
"Warns about disposed objects before returned asyncs/tasks are run"
[<Literal>]
let HelpUri =
"https://g-research.github.io/fsharp-analyzers/analyzers/DisposedBeforeAsyncRunAnalyzer.html"
[<CliAnalyzer(Name, ShortDescription, HelpUri)>]
let disposedBeforeAsyncRunCliAnalyzer : Analyzer<CliContext> =
fun (ctx : CliContext) ->
async { return analyze ctx.SourceText ctx.ParseFileResults.ParseTree ctx.CheckFileResults }
[<EditorAnalyzer(Name, ShortDescription, HelpUri)>]
let disposedBeforeAsyncRunEditorAnalyzer : Analyzer<EditorContext> =
fun (ctx : EditorContext) ->
async {
return
ctx.CheckFileResults
|> Option.map (analyze ctx.SourceText ctx.ParseFileResults.ParseTree)
|> Option.defaultValue []
}