Logo ionide-analyzers

ReturnStructPartialActivePatternAnalyzer

Problem

Since F# 6.0 you can use struct representations for partial active patterns.
The net result is that allocations are reduced. Whether you really want this will depend on your own code.

// Analyzer will trigger.
let (|Int|_|) str =
   match System.Int32.TryParse(str) with
   | true, int -> Some(int)
   | _ -> None

Fix

[<return: Struct>]
let (|Int|_|) str =
   match System.Int32.TryParse(str) with
   | true, int -> ValueSome(int)
   | _ -> ValueNone

Code fix

This analyzer has a code fix for Ionide:

code fix for ReturnStructPartialActivePattern

val str: 'a
namespace System
Represents a 32-bit signed integer.
type Int32 =
  member CompareTo: value: int -> int + 1 overload
  member Equals: obj: int -> bool + 1 overload
  member GetHashCode: unit -> int
  member GetTypeCode: unit -> TypeCode
  member ToString: unit -> string + 3 overloads
  member TryFormat: utf8Destination: Span<byte> * bytesWritten: byref<int> * ?format: ReadOnlySpan<char> * ?provider: IFormatProvider -> bool + 1 overload
  static member Abs: value: int -> int
  static member BigMul: left: int * right: int -> int64
  static member Clamp: value: int * min: int * max: int -> int
  static member CopySign: value: int * sign: int -> int
  ...
System.Int32.TryParse( s: string, result: byref<int>) : bool
System.Int32.TryParse(s: System.ReadOnlySpan<char>, result: byref<int>) : bool
System.Int32.TryParse(utf8Text: System.ReadOnlySpan<byte>, result: byref<int>) : bool
System.Int32.TryParse( s: string, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(s: System.ReadOnlySpan<char>, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(utf8Text: System.ReadOnlySpan<byte>, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse( s: string, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(s: System.ReadOnlySpan<char>, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
System.Int32.TryParse(utf8Text: System.ReadOnlySpan<byte>, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
Multiple items
val int: 'b

--------------------
type int = int32

--------------------
type int<'Measure> =
  int
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
type StructAttribute =
  inherit Attribute
  new: unit -> StructAttribute

--------------------
new: unit -> StructAttribute
active recognizer Int: 'a -> 'b option
union case ValueOption.ValueSome: 'T -> ValueOption<'T>
union case ValueOption.ValueNone: ValueOption<'T>

Type something to start searching.