Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/content/how-tos/rule-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ The following rules can be specified for linting.
- [FavourSingleton (FL0089)](rules/FL0089.html)
- [NoAsyncRunSynchronouslyInLibrary (FL0090)](rules/FL0090.html)
- [FavourNestedFunctions (FL0091)](rules/FL0091.html)
- [FavourBasicControlFlow (FL0092)](rules/FL0092.html)
30 changes: 30 additions & 0 deletions docs/content/how-tos/rules/FL0092.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: FL0092
category: how-to
hide_menu: true
---

# RecommendIfElseConstructOverMatch (FL0092)

*Introduced in `0.26.12`*

## Cause

Use of match clause when it is used in basic control flow.

## Rationale

Match clauses are more adequate for complex pattern-matching scenarios.
For simple comparisons, an if-then-else block makes terser code.

## How To Fix

Use if-else construct instead of match clause.

## Rule Settings

{
"recommendIfElseConstructOverMatch": {
"enabled": false
}
}
7 changes: 6 additions & 1 deletion src/FSharpLint.Core/Application/Configuration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ type ConventionsConfig =
suggestUseAutoProperty:EnabledConfig option
usedUnderscorePrefixedElements:EnabledConfig option
ensureTailCallDiagnosticsInRecursiveFunctions:EnabledConfig option
favourNestedFunctions:EnabledConfig option }
favourNestedFunctions:EnabledConfig option
recommendIfElseConstructOverMatch:EnabledConfig option }
with
member this.Flatten() =
Array.concat
Expand Down Expand Up @@ -416,6 +417,7 @@ with
this.ensureTailCallDiagnosticsInRecursiveFunctions |> Option.bind (constructRuleIfEnabled EnsureTailCallDiagnosticsInRecursiveFunctions.rule) |> Option.toArray
this.indexerAccessorStyleConsistency |> Option.bind (constructRuleWithConfig IndexerAccessorStyleConsistency.rule) |> Option.toArray
this.favourNestedFunctions |> Option.bind (constructRuleIfEnabled FavourNestedFunctions.rule) |> Option.toArray
this.recommendIfElseConstructOverMatch |> Option.bind (constructRuleIfEnabled RecommendIfElseConstructOverMatch.rule) |> Option.toArray
|]

[<Obsolete(ObsoleteMsg, ObsoleteWarnTreatAsError)>]
Expand Down Expand Up @@ -486,6 +488,7 @@ type Configuration =
RedundantNewKeyword:EnabledConfig option
FavourNonMutablePropertyInitialization:EnabledConfig option
FavourReRaise:EnabledConfig option
RecommendIfElseConstructOverMatch:EnabledConfig option
FavourStaticEmptyFields:EnabledConfig option
AsyncExceptionWithoutReturn:EnabledConfig option
UnneededRecKeyword:EnabledConfig option
Expand Down Expand Up @@ -589,6 +592,7 @@ with
RedundantNewKeyword = None
FavourNonMutablePropertyInitialization = None
FavourReRaise = None
RecommendIfElseConstructOverMatch = None
FavourStaticEmptyFields = None
AsyncExceptionWithoutReturn = None
UnneededRecKeyword = None
Expand Down Expand Up @@ -868,6 +872,7 @@ let flattenConfig (config:Configuration) =
config.FavourSingleton |> Option.bind (constructRuleIfEnabled FavourSingleton.rule)
config.NoAsyncRunSynchronouslyInLibrary |> Option.bind (constructRuleIfEnabled NoAsyncRunSynchronouslyInLibrary.rule)
config.FavourNestedFunctions |> Option.bind (constructRuleIfEnabled FavourNestedFunctions.rule)
config.RecommendIfElseConstructOverMatch |> Option.bind (constructRuleIfEnabled RecommendIfElseConstructOverMatch.rule)
|]

findDeprecation config deprecatedAllRules allRules
1 change: 1 addition & 0 deletions src/FSharpLint.Core/FSharpLint.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Rules\Conventions\FailwithBadUsage.fs" />
<Compile Include="Rules\Conventions\FavourSingleton.fs" />
<Compile Include="Rules\Conventions\FavourNestedFunctions.fs" />
<Compile Include="Rules\Conventions\RecommendIfElseConstructOverMatch.fs" />
<Compile Include="Rules\Conventions\SourceLength\SourceLengthHelper.fs" />
<Compile Include="Rules\Conventions\SourceLength\MaxLinesInLambdaFunction.fs" />
<Compile Include="Rules\Conventions\SourceLength\MaxLinesInMatchLambdaFunction.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module FSharpLint.Rules.RecommendIfElseConstructOverMatch

open FSharpLint.Framework
open FSharpLint.Framework.Suggestion
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open FSharpLint.Framework.Ast
open FSharpLint.Framework.Rules

let runner args =
let isBasicControlFlow (synMatchClauses: List<SynMatchClause>) =
let (|Wildcard|_|) clause =
match clause with
| SynMatchClause(SynPat.Wild _, None, _, _, _, _) -> true
| _ -> false

match synMatchClauses with
| [ SynMatchClause(SynPat.Null(_), _, _, _, _, _); Wildcard ]
| [ SynMatchClause(SynPat.Const(_), _, _, _, _, _); Wildcard ]
| [ SynMatchClause(SynPat.Named(SynIdent.SynIdent(_), _, _, _), None, _, _, _, _); Wildcard ]
| [ SynMatchClause(SynPat.LongIdent(SynLongIdent(_), _, _, SynArgPats.Pats [], _, _), _, _, _, _, _); Wildcard ] ->
true
| _ -> false

match args.AstNode with
| AstNode.Expression(SynExpr.Match (_, _, synMatchClauses, range, _)) when isBasicControlFlow synMatchClauses ->
{ Range = range
Message = Resources.GetString "RecommendIfElseConstructOverMatch"
SuggestedFix = None
TypeChecks = List.empty }
|> Array.singleton
| _ -> Array.empty

let rule =
{ Name = "RecommendIfElseConstructOverMatch"
Identifier = Identifiers.RecommendIfElseConstructOverMatch
RuleConfig = { AstNodeRuleConfig.Runner = runner; Cleanup = ignore } }
|> AstNodeRule
1 change: 1 addition & 0 deletions src/FSharpLint.Core/Rules/Identifiers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ let IndexerAccessorStyleConsistency = identifier 88
let FavourSingleton = identifier 89
let NoAsyncRunSynchronouslyInLibrary = identifier 90
let FavourNestedFunctions = identifier 91
let RecommendIfElseConstructOverMatch = identifier 92
3 changes: 3 additions & 0 deletions src/FSharpLint.Core/Text.resx
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,7 @@
<data name="RulesFavourNestedFunctions" xml:space="preserve">
<value>Prefer using local functions over private module-level functions</value>
</data>
<data name="RecommendIfElseConstructOverMatch" xml:space="preserve">
<value>Rather use simpler if-else construct instead of match clause.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/FSharpLint.Core/fsharplint.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
"avoidTooShortNames": { "enabled": false },
"asyncExceptionWithoutReturn": { "enabled": false },
"unneededRecKeyword": { "enabled": true },
"recommendIfElseConstructOverMatch": { "enabled": false },
"indentation": {
"enabled": false
},
Expand Down
1 change: 1 addition & 0 deletions tests/FSharpLint.Core.Tests/FSharpLint.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="Rules\Conventions\NoPartialFunctions.fs" />
<Compile Include="Rules\Conventions\FavourReRaise.fs" />
<Compile Include="Rules\Conventions\FavourConsistentThis.fs" />
<Compile Include="Rules\Conventions\RecommendIfElseConstructOverMatch.fs" />
<Compile Include="Rules\Conventions\AvoidTooShortNames.fs" />
<Compile Include="Rules\Conventions\AvoidSinglePipeOperator.fs" />
<Compile Include="Rules\Conventions\SuggestUseAutoProperty.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module FSharpLint.Core.Tests.Rules.Conventions.RecommendIfElseConstructOverMatch

open NUnit.Framework
open FSharpLint.Rules
open FSharpLint.Core.Tests

[<TestFixture>]
type TestConventionsRecommendIfElseConstructOverMatch() =
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(RecommendIfElseConstructOverMatch.rule)


[<Test>]
member this.MoreThanTwoClausesShouldNotProduceError() =
this.Parse """
match foo with
| bar -> ()
| baz -> ()
| _ -> () """

Assert.IsTrue this.NoErrorsExist


[<Test>]
member this.TwoClausesWithoutWildcardShouldNotProduceError() =
this.Parse """
match foo with
| bar -> ()
| baz -> () """

Assert.IsTrue this.NoErrorsExist

[<Test>]
member this.ThePresenceOfDUShouldNotProduceError() =
this.Parse """
match foo with
| Bar baz -> ()
| _ -> () """

Assert.IsTrue this.NoErrorsExist

[<Test>]
member this.ThePresenceOfWhenClauseShouldNotProduceError() =
this.Parse """
match foo with
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webwarrior-ws this cannot be written as if foo = Baz.Bar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it can. Though in this situation pattern matching seems more natural choice.
So should I remove this case from exceptions to the rule?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes let's remove it because I want to see what's the impact on SelfCheck; keep your change around in case we put it back

| bar when bar.Count = 0 -> ()
| _ -> ()"""

Assert.IsTrue this.NoErrorsExist

[<Test>]
member this.TwoClausesWithWildcardShouldProduceError() =
this.Parse """
match foo with
| bar -> ()
| _ -> () """

Assert.IsTrue this.ErrorsExist

[<Test>]
member this.TwoClausesWithNullAndWildcardShouldProduceError() =
this.Parse """
match foo with
| null -> ()
| _ -> () """

Assert.IsTrue this.ErrorsExist

[<Test>]
member this.TwoClausesWithNullAndVariableShouldNotProduceError() =
this.Parse """
match foo with
| null -> ()
| notNullFoo -> () """

Assert.IsTrue this.NoErrorsExist
Loading