Skip to content

Commit 6e3a03e

Browse files
authored
Merge pull request #85 from nojaf/good-stuff
Add analyzers from FSharp.Formatting PR
2 parents 0e830a4 + a56e42c commit 6e3a03e

32 files changed

+2061
-51
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 0.10.0 - 2024-03-29
4+
5+
### Added
6+
* HeadConsEmptyListPatternAnalyzer. [#85](https://github.com/ionide/ionide-analyzers/pull/85)
7+
* ListEqualsEmptyListAnalyzer. [#85](https://github.com/ionide/ionide-analyzers/pull/85)
8+
* ReturnStructPartialActivePatternAnalyzer [#85](https://github.com/ionide/ionide-analyzers/pull/85)
9+
* CombinePipedModuleFunctionsAnalyzer [#85](https://github.com/ionide/ionide-analyzers/pull/85)
10+
* EqualsNullAnalyzer [#85](https://github.com/ionide/ionide-analyzers/pull/85)
11+
* StructDiscriminatedUnionAnalyzer. [#85](https://github.com/ionide/ionide-analyzers/pull/85)
12+
313
## 0.9.0 - 2024-02-15
414

515
### Changed

build.fsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,11 @@ pipeline "NewAnalyzer" {
364364
let analyzerContent =
365365
$"""module Ionide.Analyzers.%s{category}.%s{analyzerName}
366366
367+
open System.Collections.Generic
367368
open FSharp.Compiler.Symbols
368369
open FSharp.Compiler.Text
369370
open FSharp.Compiler.Syntax
371+
open FSharp.Compiler.SyntaxTrivia
370372
open FSharp.Analyzers.SDK
371373
open FSharp.Analyzers.SDK.ASTCollecting
372374
open FSharp.Analyzers.SDK.TASTCollecting

docs/img/EqualsNullAnalyzer.gif

26.5 KB
Loading
30.9 KB
Loading
42.6 KB
Loading
44.5 KB
Loading
31.6 KB
Loading

docs/performance/008.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: ListEqualsEmptyListAnalyzer
3+
category: performance
4+
categoryindex: 4
5+
index: 1
6+
---
7+
8+
# ListEqualsEmptyListAnalyzer
9+
10+
## Problem
11+
12+
It is less performant to check if a list is empty by using the `=` operator.
13+
14+
```fsharp
15+
let a = [ 1; 2; 3 ]
16+
// Analyzer will trigger
17+
let b = [] = a
18+
```
19+
20+
## Fix
21+
22+
```fsharp
23+
let b = List.isEmpty a // will check if there is no tail which is faster than the equality check.
24+
```
25+
26+
## Code fix
27+
28+
This analyzer has a code fix for Ionide:
29+
30+
![Code fix for ListEqualsEmptyList](../img/ListEqualsEmptyListAnalyzer.gif)

docs/performance/009.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: ReturnStructPartialActivePatternAnalyzer
3+
category: performance
4+
categoryindex: 4
5+
index: 2
6+
---
7+
8+
# ReturnStructPartialActivePatternAnalyzer
9+
10+
## Problem
11+
12+
Since F# 6.0 you can use [struct representations for partial active patterns](https://learn.microsoft.com/en-us/dotnet/fsharp/whats-new/fsharp-6#struct-representations-for-partial-active-patterns).
13+
The net result is that allocations are reduced. Whether you really want this will depend on your own code.
14+
15+
```fsharp
16+
// Analyzer will trigger.
17+
let (|Int|_|) str =
18+
match System.Int32.TryParse(str) with
19+
| true, int -> Some(int)
20+
| _ -> None
21+
```
22+
23+
## Fix
24+
25+
```fsharp
26+
[<return: Struct>]
27+
let (|Int|_|) str =
28+
match System.Int32.TryParse(str) with
29+
| true, int -> ValueSome(int)
30+
| _ -> ValueNone
31+
```
32+
33+
## Code fix
34+
35+
This analyzer has a code fix for Ionide:
36+
37+
![code fix for ReturnStructPartialActivePattern](../img/ReturnStructPartialActivePatternAnalyzer.gif)

docs/performance/010.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: CombinePipedModuleFunctionsAnalyzer
3+
category: performance
4+
categoryindex: 4
5+
index: 3
6+
---
7+
8+
# CombinePipedModuleFunctionsAnalyzer
9+
10+
## Problem
11+
12+
Subsequent collection functions like `List.filter` and `List.map` can sometimes be combined into `List.choose`.
13+
14+
```fsharp
15+
let a b =
16+
b
17+
|> List.filter (fun x -> x < 20)
18+
|> List.map (fun y -> y * 2)
19+
```
20+
21+
## Fix
22+
23+
```fsharp
24+
let a b =
25+
b
26+
|> List.choose (fun x -> if x < 20 then Some(x * 2) else None)
27+
```

0 commit comments

Comments
 (0)