From 1ec0f7aefeccac3bcdb8f1ceb700e7ce7c0632ea Mon Sep 17 00:00:00 2001 From: ijanus Date: Tue, 28 Jun 2022 13:15:17 +0100 Subject: [PATCH 1/2] RedundantNewKeyword: add more unit test Related to https://github.com/fsprojects/FSharpLint/issues/555. --- .../Rules/Conventions/RedundantNewKeyword.fs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/FSharpLint.Core.Tests/Rules/Conventions/RedundantNewKeyword.fs b/tests/FSharpLint.Core.Tests/Rules/Conventions/RedundantNewKeyword.fs index 7cd38e263..f82d4e80d 100644 --- a/tests/FSharpLint.Core.Tests/Rules/Conventions/RedundantNewKeyword.fs +++ b/tests/FSharpLint.Core.Tests/Rules/Conventions/RedundantNewKeyword.fs @@ -38,4 +38,31 @@ module Program let _ = System.Version()""" this.Parse source - Assert.AreEqual(expected, this.ApplyQuickFix source) \ No newline at end of file + Assert.AreEqual(expected, this.ApplyQuickFix source) + + [] + member this.``new keyword is not required (1).``() = + this.Parse(""" +module Program + let foo = + new System.Collections.Generic.Dictionary() |> ignore""") + + Assert.IsTrue this.ErrorsExist + + [] + member this.``new keyword is not required (2).``() = + this.Parse(""" +module Program + let foo = + new Guid() |> ignore""") + + Assert.IsTrue this.ErrorsExist + + [] + member this.``new keyword is not required (3).``() = + this.Parse(""" +module Program + let foo = + new Int32() |> ignore""") + + Assert.IsTrue this.ErrorsExist From edb3d87693e37aa9dcae0e098ae02d2c0b3c40f6 Mon Sep 17 00:00:00 2001 From: ijanus Date: Wed, 29 Jun 2022 11:09:00 +0100 Subject: [PATCH 2/2] RedundantNewKeyword: fix failing unit test Fixes https://github.com/fsprojects/FSharpLint/issues/555. --- .../Rules/Conventions/RedundantNewKeyword.fs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/FSharpLint.Core/Rules/Conventions/RedundantNewKeyword.fs b/src/FSharpLint.Core/Rules/Conventions/RedundantNewKeyword.fs index 5d96b40ee..6ba4f3cb5 100644 --- a/src/FSharpLint.Core/Rules/Conventions/RedundantNewKeyword.fs +++ b/src/FSharpLint.Core/Rules/Conventions/RedundantNewKeyword.fs @@ -27,7 +27,11 @@ let private doesNotImplementIDisposable (checkFile:FSharpCheckFileResults) (iden ctor.DeclaringEntity |> Option.exists (fun ctorForType -> Seq.forall (implementsIDisposable >> not) ctorForType.AllInterfaces) - | Some(_) | None -> false + | Some symbol when (symbol.Symbol :? FSharpEntity) -> + let ctor = symbol.Symbol :?> FSharpEntity + Seq.forall (implementsIDisposable >> not) ctor.AllInterfaces + | Some _ -> false + | None -> true let private generateFix (text:string) range = lazy( ExpressionUtilities.tryFindTextOfRange range text @@ -38,8 +42,9 @@ let private generateFix (text:string) range = lazy( let runner args = - match (args.AstNode, args.CheckInfo) with - | (AstNode.Expression(SynExpr.New(_, SynType.LongIdent(identifier), _, range)), Some checkInfo) -> + match args.AstNode, args.CheckInfo with + | AstNode.Expression(SynExpr.New(_, SynType.LongIdent(identifier), _, range)), Some checkInfo + | AstNode.Expression(SynExpr.New(_, SynType.App(SynType.LongIdent(identifier), _, _, _, _, _, _), _, range)), Some checkInfo -> { Range = range Message = Resources.GetString("RulesRedundantNewKeyword") SuggestedFix = Some (generateFix args.FileContent range)