From 29c686ab0a415af2a562ce6c746686902ea9f1e1 Mon Sep 17 00:00:00 2001 From: Mansheng Yang Date: Tue, 31 Jul 2012 21:43:46 +0800 Subject: [PATCH] [CodeIssue] Added DoubleNegationIssue --- .../ICSharpCode.NRefactory.CSharp.csproj | 5 +- .../CodeIssues/DoubleNegationIssue.cs | 78 +++++++++++++++++++ .../CodeIssues/DoubleNegationIssueTests.cs | 63 +++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 5 +- 4 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DoubleNegationIssue.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DoubleNegationIssueTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index ce4c248317..ae45970b32 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -52,7 +52,7 @@ False False DEBUG;TRACE;FULL_AST;NET45 - v4.5 + v4.5 full @@ -63,7 +63,7 @@ True False TRACE;FULL_AST;NET45 - v4.5 + v4.5 PdbOnly @@ -290,6 +290,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DoubleNegationIssue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DoubleNegationIssue.cs new file mode 100644 index 0000000000..86fd3452d8 --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DoubleNegationIssue.cs @@ -0,0 +1,78 @@ +// +// DoubleNegationIssue.cs +// +// Author: +// Mansheng Yang +// +// Copyright (c) 2012 Mansheng Yang +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.Collections.Generic; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + [IssueDescription ("Double negation is meaningless", + Description = "Double negation is meaningless.", + Category = IssueCategories.Redundancies, + Severity = Severity.Warning, + IssueMarker = IssueMarker.Underline)] + public class DoubleNegationIssue : ICodeIssueProvider + { + public IEnumerable GetIssues (BaseRefactoringContext context) + { + return new GatherVisitor (context).GetIssues (); + } + + class GatherVisitor : GatherVisitorBase + { + public GatherVisitor (BaseRefactoringContext ctx) + : base (ctx) + { + } + + static Expression RemoveParentheses (Expression expr) + { + while (expr is ParenthesizedExpression) + expr = ((ParenthesizedExpression) expr).Expression; + return expr; + } + + public override void VisitUnaryOperatorExpression (UnaryOperatorExpression unaryOperatorExpression) + { + base.VisitUnaryOperatorExpression (unaryOperatorExpression); + + if (unaryOperatorExpression.Operator != UnaryOperatorType.Not) + return; + + var innerUnaryOperatorExpr = + RemoveParentheses (unaryOperatorExpression.Expression) as UnaryOperatorExpression; + if (innerUnaryOperatorExpr == null || innerUnaryOperatorExpr.Operator != UnaryOperatorType.Not) + return; + + var expression = RemoveParentheses (innerUnaryOperatorExpr.Expression); + if (expression.IsNull) + return; + + AddIssue (unaryOperatorExpression, ctx.TranslateString ("Remove double negation"), + script => script.Replace (unaryOperatorExpression, expression.Clone ())); + } + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DoubleNegationIssueTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DoubleNegationIssueTests.cs new file mode 100644 index 0000000000..8d9136101b --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DoubleNegationIssueTests.cs @@ -0,0 +1,63 @@ +// +// DoubleNegationIssueTests.cs +// +// Author: +// Mansheng Yang +// +// Copyright (c) 2012 Mansheng Yang +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using ICSharpCode.NRefactory.CSharp.Refactoring; +using NUnit.Framework; + +namespace ICSharpCode.NRefactory.CSharp.CodeIssues +{ + [TestFixture] + public class DoubleNegationIssueTests : InspectionActionTestBase + { + [Test] + public void Test () + { + var input = @" +class TestClass +{ + bool GetBool () { } + + void TestMethod () + { + var x = !!GetBool (); + x = !(!(GetBool ())); + } +}"; + var output = @" +class TestClass +{ + bool GetBool () { } + + void TestMethod () + { + var x = GetBool (); + x = GetBool (); + } +}"; + Test (input, 2, output); + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 70a342128e..1b2c864bff 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -72,7 +72,7 @@ False True DEBUG;TRACE;NET45 - v4.5 + v4.5 ..\ICSharpCode.NRefactory\bin\Debug\ @@ -93,7 +93,7 @@ True False TRACE;NET45 - v4.5 + v4.5 ..\ICSharpCode.NRefactory\bin\Release\ @@ -149,6 +149,7 @@ +