-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from sheridanchris/option-get-analyzer
Add an analyzer that detects Option.get usage
- Loading branch information
Showing
6 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: ReplaceOptionGetWithGracefulHandling | ||
category: suggestion | ||
categoryindex: 2 | ||
index: 5 | ||
--- | ||
# OptionGetAnalyzer | ||
|
||
## Problem | ||
The option type is used to model a potentially missing value. Unwrapping an `'a option` into an `'a` by using `Option.get` circumvents the graceful handling of the `None` case. You should unwrap the value using a function like `Option.defaultValue` or pattern matching instead. | ||
|
||
```fsharp | ||
let option = None | ||
let value = Option.get option // Triggers analyzer | ||
``` | ||
|
||
## Fix | ||
|
||
Gracefully handle the option value by accounting for both cases. | ||
|
||
```fsharp | ||
let option = None | ||
match option with | ||
| Some value -> () | ||
| None -> () | ||
// or | ||
Option.defaultValue () option | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Ionide.Analyzers/Suggestion/ReplaceOptionGetWithGracefulHandlingAnalyzer.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Ionide.Analyzers.Suggestion.ReplaceOptionGetWithGracefulHandlingAnalyzer | ||
|
||
open FSharp.Analyzers.SDK | ||
open FSharp.Analyzers.SDK.TASTCollecting | ||
open FSharp.Compiler.Symbols | ||
open FSharp.Compiler.Text | ||
|
||
[<CliAnalyzer("ReplaceOptionGetWithGracefulHandlingAnalyzer", | ||
"Replace Option.get with graceful handling of each case.", | ||
"https://ionide.io/ionide-analyzers/suggestion/006.html")>] | ||
let optionGetAnalyzer (ctx: CliContext) = | ||
async { | ||
let messages = ResizeArray<Message>() | ||
|
||
let walker = | ||
{ new TypedTreeCollectorBase() with | ||
override x.WalkCall _ (mfv: FSharpMemberOrFunctionOrValue) _ _ (args: FSharpExpr list) (m: range) = | ||
if mfv.FullName = "Microsoft.FSharp.Core.Option.get" && args.Length = 1 then | ||
messages.Add | ||
{ | ||
Type = "ReplaceOptionGetWithGracefulHandlingAnalyzer" | ||
Message = "Replace Option.get with graceful handling of each case." | ||
Code = "IONIDE-006" | ||
Severity = Severity.Hint | ||
Range = m | ||
Fixes = [] | ||
} | ||
} | ||
|
||
match ctx.TypedTree with | ||
| None -> return [] | ||
| Some typedTree -> | ||
walkTast walker typedTree | ||
return Seq.toList messages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/Ionide.Analyzers.Tests/Suggestion/ReplaceOptionGetWithGracefulHandlingAnalyzerTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Ionide.Analyzers.Tests.Suggestion.ReplaceOptionGetWithGracefulHandlingAnalyzerTests | ||
|
||
open NUnit.Framework | ||
open FSharp.Compiler.CodeAnalysis | ||
open FSharp.Analyzers.SDK.Testing | ||
open Ionide.Analyzers.Suggestion.ReplaceOptionGetWithGracefulHandlingAnalyzer | ||
|
||
let mutable projectOptions: FSharpProjectOptions = FSharpProjectOptions.zero | ||
|
||
[<SetUp>] | ||
let Setup () = | ||
task { | ||
let! opts = mkOptionsFromProject "net7.0" [] | ||
projectOptions <- opts | ||
} | ||
|
||
[<Test>] | ||
let ``Option.get is detected`` () = | ||
async { | ||
let source = | ||
""" | ||
module M | ||
let option = Some 10 | ||
let value = Option.get option | ||
""" | ||
|
||
let ctx = getContext projectOptions source | ||
let! msgs = optionGetAnalyzer ctx | ||
Assert.IsNotEmpty msgs | ||
Assert.IsTrue(Assert.messageContains "Replace Option.get with graceful handling of each case." msgs[0]) | ||
} |