Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Favor Async.Ignore with type #581

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/content/how-tos/rules/FL0070.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ hide_menu: true

## Cause

Use of `ignore` function is untyped.
Use of `ignore` or `Async.Ignore` function is untyped.

## Rationale

Specifying a type prevents mistakes and aides maintainability.

## How To Fix

Add the type of the element being ignored as a type parameter to the `ignore`
invokation, e.g. `ignore<string>`.
Add the type of the element being ignored as a type parameter to the
invokation, e.g. `ignore<string>`, `Async.Ignore<int>`.

## Rule Settings

Expand Down
21 changes: 21 additions & 0 deletions src/FSharpLint.Core/Rules/Conventions/Binding/FavourTypedIgnore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ let private runner (args: AstNodeRuleParams) =
Message = String.Format(Resources.GetString "RulesFavourTypedIgnore", identifier)
SuggestedFix = Some suggestedFix
TypeChecks = [] }

let generateErrorAsyncIgnore (ident: LongIdent) (range) =
if ident.[0].ToString().Equals("Async")
&& ident.[1].ToString().Equals("Ignore") then
{ Range = range
Message = Resources.GetString "RulesAsyncIgnoreWithType"
SuggestedFix = None
TypeChecks = List.Empty } |> Array.singleton
else
Array.empty

let isTyped expression identifier range text =
match expression with
Expand All @@ -46,6 +56,17 @@ let private runner (args: AstNodeRuleParams) =
| _ ->
generateError identifier.idText range identifier.idText
|> Array.singleton
| AstNode.Expression(SynExpr.DoBang(expr, _)) ->
match expr with
| SynExpr.App(_, _, _, ignoreExpr, _) ->
match ignoreExpr with
| SynExpr.LongIdent(_,LongIdentWithDots(idents, _), _, range) ->
generateErrorAsyncIgnore idents range
| _ -> Array.empty
| _ ->
Array.empty
| _ ->
Array.empty
| _ -> Array.empty

/// Checks if any code uses untyped ignore
Expand Down
3 changes: 3 additions & 0 deletions src/FSharpLint.Core/Text.resx
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,7 @@
<data name="RulesFavourStaticEmptyFieldsForArray" xml:space="preserve">
<value>Consider using 'Array.empty' instead.</value>
</data>
<data name="RulesAsyncIgnoreWithType" xml:space="preserve">
<value>You should use Async.Ignore with specified type</value>
</data>
</root>
52 changes: 52 additions & 0 deletions tests/FSharpLint.Core.Tests/Rules/Binding/FavourTypedIgnore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,55 @@ ignore(

Assert.IsTrue(this.ErrorsExist)
Assert.IsTrue(this.ErrorExistsAt(2, 0))

[<Test>]
member this.AsyncIgnoreWithoutType() =
this.Parse
"""
namespace Program

module X =
let f x =
do! x() |> Async.Ignore"""
Assert.IsTrue this.ErrorsExist

[<Test>]
member this.AsyncIgnoreWithType() =
this.Parse
"""
namespace Program

module X =
let f x =
do! x() |> Async.Ignore<int>"""
Assert.IsTrue this.NoErrorsExist

[<Test>]
member this.AsyncIgnoreComplexWithoutType() =
this.Parse
"""
namespace Program

module X =
let f x =
do!
x()
|> UnwrapResult
|> Async.AwaitTask
|> Async.Ignore"""
Assert.IsTrue this.ErrorsExist

[<Test>]
member this.AsyncIgnoreComplexWithtType() =
this.Parse
"""
namespace Program

module X =
let f x =
do!
x()
|> UnwrapResult
|> Async.AwaitTask
|> Async.Ignore<int>"""
Assert.IsTrue this.NoErrorsExist