From 8e94126c11c77006c2f32407091222a38148b7b7 Mon Sep 17 00:00:00 2001 From: Simon Lindgren Date: Tue, 5 Jun 2012 11:13:43 +0200 Subject: [PATCH] [CodeIssue] Add IncorrectExceptionParameterOrderingIssue. --- .../ICSharpCode.NRefactory.CSharp.csproj | 1 + ...ncorrectExceptionParameterOrderingIssue.cs | 92 +++++++++++++++++++ ...ncorrectExceptionParameterOrderingTests.cs | 90 ++++++++++++++++++ .../ICSharpCode.NRefactory.Tests.csproj | 1 + 4 files changed, 184 insertions(+) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/IncorrectExceptionParameterOrderingIssue.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/IncorrectExceptionParameterOrderingTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index 31a81b196c..01b9c83a2f 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -376,6 +376,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/IncorrectExceptionParameterOrderingIssue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/IncorrectExceptionParameterOrderingIssue.cs new file mode 100644 index 0000000000..80ab444db6 --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/IncorrectExceptionParameterOrderingIssue.cs @@ -0,0 +1,92 @@ +// +// IncorrectExceptionParametersOrderingIssue.cs +// +// Author: +// Simon Lindgren +// +// Copyright (c) 2012 Simon Lindgren +// +// 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 System.Collections.Generic; +using ICSharpCode.NRefactory.Semantics; +using System; + +namespace ICSharpCode.NRefactory.CSharp +{ + [IssueDescription("Incorrect ordering of exception constructor parameters", + Description = "Warns about the constructor parameter ordering of some confusing exception types.", + Category = IssueCategories.CodeQualityIssues, + Severity = Severity.Warning)] + public class IncorrectExceptionParameterOrderingIssue : ICodeIssueProvider + { + public IEnumerable GetIssues(BaseRefactoringContext context) + { + return new GatherVisitor(context).GetIssues(); + } + + class GatherVisitor : GatherVisitorBase + { + readonly BaseRefactoringContext context; + Dictionary> rules; + + public GatherVisitor(BaseRefactoringContext context) : base (context) + { + this.context = context; + rules = new Dictionary>(); + new ArgumentNullException(); + rules [typeof(ArgumentException).FullName] = (left, right) => left > right; + rules [typeof(ArgumentNullException).FullName] = (left, right) => left < right; + rules [typeof(ArgumentOutOfRangeException).FullName] = (left, right) => left < right; + rules [typeof(DuplicateWaitObjectException).FullName] = (left, right) => left < right; + } + + public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression) + { + var type = context.Resolve(objectCreateExpression.Type) as TypeResolveResult; + if (type == null) + return; + var parameters = objectCreateExpression.Arguments; + if (parameters.Count != 2) + return; + var firstParam = parameters.FirstOrNullObject() as PrimitiveExpression; + var secondParam = parameters.LastOrNullObject() as PrimitiveExpression; + if (firstParam == null || firstParam.Value.GetType() != typeof(string) || + secondParam == null || firstParam.Value.GetType() != typeof(string)) + return; + var leftLength = (firstParam.Value as string).Length; + var rightLength = (secondParam.Value as string).Length; + + Func func; + if (!rules.TryGetValue(type.Type.FullName, out func)) + return; + if (!func(leftLength, rightLength)) + AddIssue(objectCreateExpression, + context.TranslateString("Swap parameters"), + script => { + var newOCE = objectCreateExpression.Clone() as ObjectCreateExpression; + newOCE.Arguments.Clear(); + newOCE.Arguments.Add(secondParam.Clone()); + newOCE.Arguments.Add(firstParam.Clone()); + script.Replace(objectCreateExpression, newOCE); + }); + } + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/IncorrectExceptionParameterOrderingTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/IncorrectExceptionParameterOrderingTests.cs new file mode 100644 index 0000000000..6117d5f969 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/IncorrectExceptionParameterOrderingTests.cs @@ -0,0 +1,90 @@ +// +// IncorrectExceptionParametersOrderingTests.cs +// +// Author: +// Simon Lindgren +// +// Copyright (c) 2012 Simon Lindgren +// +// 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; +using ICSharpCode.NRefactory.CSharp.CodeIssues; +using NUnit.Framework; +using ICSharpCode.NRefactory.CSharp.CodeActions; +using ICSharpCode.NRefactory.CSharp; + +namespace ICSharpCode.NRefactory.CSharp.CodeIssues +{ + public class IncorrectExceptionParameterOrderingTests : InspectionActionTestBase + { + + [Test] + public void TestBadExamples() + { + var input = @" +using System; +class A +{ + void F() + { + throw new ArgumentNullException (""The parameter 'blah' can not be null"", ""blah""); + throw new ArgumentException (""blah"", ""The parameter 'blah' can not be null""); + throw new ArgumentOutOfRangeException (""The parameter 'blah' can not be null"", ""blah""); + throw new DuplicateWaitObjectException (""The parameter 'blah' can not be null"", ""blah""); + } +}"; + TestRefactoringContext context; + var issues = GetIssues(new IncorrectExceptionParameterOrderingIssue(), input, out context); + Assert.AreEqual(4, issues.Count); + + CheckFix(context, issues [0], @" +using System; +class A +{ + void F() + { + throw new ArgumentNullException (""blah"", ""The parameter 'blah' can not be null""); + throw new ArgumentException (""blah"", ""The parameter 'blah' can not be null""); + throw new ArgumentOutOfRangeException (""The parameter 'blah' can not be null"", ""blah""); + throw new DuplicateWaitObjectException (""The parameter 'blah' can not be null"", ""blah""); + } +}"); + } + + [Test] + public void TestGoodExamples() + { + var input = @" +using System; +class A +{ + void F() + { + throw new ArgumentNullException (""blah"", ""The parameter 'blah' can not be null""); + throw new ArgumentException (""The parameter 'blah' can not be null"", ""blah""); + throw new ArgumentOutOfRangeException (""blah"", ""The parameter 'blah' can not be null""); + throw new DuplicateWaitObjectException (""blah"", ""The parameter 'blah' can not be null""); + } +}"; + TestRefactoringContext context; + var issues = GetIssues(new IncorrectExceptionParameterOrderingIssue(), input, out context); + Assert.AreEqual(0, issues.Count); + } + } +} diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 47de767cf8..3f1186836a 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -272,6 +272,7 @@ +