Skip to content
Merged
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
46 changes: 31 additions & 15 deletions src/FSharpLint.Core/Rules/Conventions/SuggestUseAutoProperty.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ and isImmutableSequentialExpression args expression =
|| isImmutableValueExpression args expr2)
| _ -> false

let private hasStructAttribute node =
match node with
| AstNode.TypeDefinition(SynTypeDefn(SynComponentInfo(attributes, _, _, _, _, _, _, _), _, _, _, _, _)) ->
attributes
|> extractAttributes
|> List.exists
(fun attribute ->
match List.tryLast attribute.TypeName.LongIdent with
| Some(ident) -> ident.idText = "Struct" || ident.idText = "StructAttribute"
| None -> false)
| _ -> false

let private runner (args: AstNodeRuleParams) =
match args.AstNode with
| MemberDefinition
Expand Down Expand Up @@ -73,21 +85,25 @@ let private runner (args: AstNodeRuleParams) =
| _, SynArgPats.Pats pats when pats.Length > 0 -> // non-property member
Array.empty
| expression, _ when isImmutableValueExpression args expression ->
let suggestedFix =
lazy
(match memberIdentifier.LongIdent with
| [ _; memberName ] ->
Some
{ FromText = args.FileContent
FromRange = memberIdentifier.Range
ToText = $"val {memberName.idText}" }
| _ -> None)

{ Range = memberRange
Message = Resources.GetString "RulesSuggestUseAutoProperty"
SuggestedFix = Some suggestedFix
TypeChecks = List.Empty }
|> Array.singleton
match args.GetParents args.NodeIndex with
| parentNode :: _ when hasStructAttribute parentNode ->
Array.empty
| _ ->
let suggestedFix =
lazy
(match memberIdentifier.LongIdent with
| [ _; memberName ] ->
Some
{ FromText = args.FileContent
FromRange = memberIdentifier.Range
ToText = $"val {memberName.idText}" }
| _ -> None)

{ Range = memberRange
Message = Resources.GetString "RulesSuggestUseAutoProperty"
SuggestedFix = Some suggestedFix
TypeChecks = List.Empty }
|> Array.singleton
| _ -> Array.empty
| _ -> Array.empty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,13 @@ type Foo(content: int) =

this.Parse source
Assert.AreEqual(expected, this.ApplyQuickFix source)

[<Test>]
member this.``Should not suggest using auto-property for types with [<Struct>] attribute`` () =
this.Parse """
[<Struct>]
type Foo(content: int) =
member self.Content = content
"""

Assert.IsTrue this.NoErrorsExist
Loading