-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule UnneededRecKeyword. Fixes #650
- Loading branch information
Showing
11 changed files
with
136 additions
and
2 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,29 @@ | ||
--- | ||
title: FL0083 | ||
category: how-to | ||
hide_menu: true | ||
--- | ||
|
||
# UnneededRecKeyword (FL0083) | ||
|
||
*Introduced in `0.23.8`* | ||
|
||
## Cause | ||
|
||
Recursive function (function marked with a "rec" keyword) does not invoke itself. | ||
|
||
## Rationale | ||
|
||
Using "rec" keyword on a function that is not recursive is unnecessary. | ||
|
||
## How To Fix | ||
|
||
Update the function to invoke itself or remove "rec" keyword in case it doesn't need to invoke itself recursively. | ||
|
||
## Rule Settings | ||
|
||
{ | ||
"unneededRecKeyword": { | ||
"enabled": true | ||
} | ||
} |
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
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
48 changes: 48 additions & 0 deletions
48
src/FSharpLint.Core/Rules/Conventions/UnneededRecKeyword.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,48 @@ | ||
module FSharpLint.Rules.UnneededRecKeyword | ||
|
||
open System | ||
open FSharp.Compiler.Syntax | ||
open FSharpLint.Framework.Ast | ||
open FSharpLint.Framework.Rules | ||
open FSharpLint.Framework | ||
open FSharpLint.Framework.Suggestion | ||
|
||
let runner (args: AstNodeRuleParams) = | ||
match args.AstNode, args.CheckInfo with | ||
| AstNode.ModuleDeclaration (SynModuleDecl.Let (isRecursive, bindings, letRange)), Some checkInfo when isRecursive -> | ||
match bindings with | ||
| SynBinding (_, _, _, _, _, _, _, SynPat.LongIdent (LongIdentWithDots([ident], _), _, _, _, _, range), _, _, _, _) :: _ -> | ||
Check failure on line 14 in src/FSharpLint.Core/Rules/Conventions/UnneededRecKeyword.fs GitHub Actions / buildAndTest (ubuntu-latest)
|
||
let symbolUses = checkInfo.GetAllUsesOfAllSymbolsInFile() | ||
let funcName = ident.idText | ||
|
||
let functionCalls = | ||
symbolUses | ||
|> Seq.filter (fun usage -> | ||
usage.Symbol.DisplayName = funcName | ||
&& usage.Range.StartLine >= letRange.StartLine | ||
&& usage.Range.EndLine <= letRange.EndLine) | ||
|
||
|
||
if (functionCalls |> Seq.length) <= 1 then | ||
{ Range = range | ||
Message = | ||
String.Format( | ||
Resources.GetString "RulesUnneededRecKeyword", | ||
funcName | ||
) | ||
SuggestedFix = None | ||
TypeChecks = list.Empty } | ||
|> Array.singleton | ||
else | ||
Array.empty | ||
| _ -> Array.empty | ||
|
||
| _ -> Array.empty | ||
|
||
let rule = | ||
{ Name = "UnneededRecKeyword" | ||
Identifier = Identifiers.UnneededRecKeyword | ||
RuleConfig = | ||
{ AstNodeRuleConfig.Runner = runner | ||
Cleanup = ignore } } | ||
|> AstNodeRule |
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
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
44 changes: 44 additions & 0 deletions
44
tests/FSharpLint.Core.Tests/Rules/Conventions/UnneededRecKeyword.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,44 @@ | ||
module FSharpLint.Core.Tests.Rules.Conventions.UnneededRecKeyword | ||
|
||
open NUnit.Framework | ||
open FSharpLint.Framework.Rules | ||
open FSharpLint.Rules | ||
|
||
[<TestFixture>] | ||
type TestConventionsUnneededRecKeyword() = | ||
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(UnneededRecKeyword.rule) | ||
|
||
[<Test>] | ||
member this.UnneededRecKeywordShouldNotProduceError() = | ||
this.Parse """ | ||
let rec Foo () = | ||
if someParam then | ||
Foo() | ||
else | ||
()""" | ||
|
||
Assert.IsTrue this.NoErrorsExist | ||
|
||
[<Test>] | ||
member this.UnneededRecKeywordShouldProduceError_1() = | ||
this.Parse """ | ||
let rec Foo someParam = | ||
()""" | ||
|
||
Assert.IsTrue this.ErrorsExist | ||
|
||
[<Test>] | ||
member this.UnneededRecKeywordShouldProduceError_2() = | ||
this.Parse """ | ||
let rec Foo someParam = | ||
() | ||
[<EntryPoint>] | ||
let main args = | ||
let Foo () = | ||
() | ||
Foo() | ||
0""" | ||
|
||
Assert.IsTrue this.ErrorsExist |