-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Amaury Levé
authored
Aug 2, 2017
1 parent
2f657cd
commit 0201b8b
Showing
10 changed files
with
281 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...nalyzer-dotnet/its/expected/Nancy/Nancy-{34576216-0DCA-4B0F-A0DC-9075E75A676F}-S3693.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"id": "S3693", | ||
"message": "Avoid throwing exceptions in this constructor.", | ||
"location": { | ||
"uri": "Nancy\src\Nancy\ModelBinding\ModelBindingException.cs", | ||
"region": { | ||
"startLine": 36, | ||
"startColumn": 17, | ||
"endLine": 36, | ||
"endColumn": 62 | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<p>It may be a good idea to raise an exception in a constructor if you're unable to fully flesh the object in question, but not in an | ||
<code>exception</code> constructor. If you do, you'll interfere with the exception that was originally being thrown. Further, it is highly unlikely | ||
that an exception raised in the creation of an exception will be properly handled in the calling code, and the unexpected, unhandled exception will | ||
lead to program termination.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
class MyException: Exception | ||
{ | ||
public void MyException() | ||
{ | ||
if (bad_thing) | ||
{ | ||
throw new Exception("A bad thing happened"); // Noncompliant | ||
} | ||
} | ||
} | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"title": "Exception constructors should not throw exceptions", | ||
"type": "BUG", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "15min" | ||
}, | ||
"tags": [ | ||
|
||
], | ||
"defaultSeverity": "Blocker" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,6 +149,7 @@ | |
"S3626", | ||
"S3649", | ||
"S3655", | ||
"S3693", | ||
"S3776", | ||
"S3869", | ||
"S3871", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/ExceptionConstructorShouldNotThrow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2017 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using SonarAnalyzer.Common; | ||
using SonarAnalyzer.Helpers; | ||
|
||
namespace SonarAnalyzer.Rules.CSharp | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
[Rule(DiagnosticId)] | ||
public sealed class ExceptionConstructorShouldNotThrow : SonarDiagnosticAnalyzer | ||
{ | ||
internal const string DiagnosticId = "S3693"; | ||
private const string MessageFormat = "Avoid throwing exceptions in this constructor."; | ||
|
||
private static readonly DiagnosticDescriptor rule = | ||
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager); | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule); | ||
|
||
protected override void Initialize(SonarAnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeActionInNonGenerated( | ||
c => | ||
{ | ||
var classDeclaration = (ClassDeclarationSyntax)c.Node; | ||
var classSymbol = c.SemanticModel.GetDeclaredSymbol(classDeclaration); | ||
if (classSymbol == null || | ||
!classSymbol.DerivesFrom(KnownType.System_Exception)) | ||
{ | ||
return; | ||
} | ||
var throwStatementsPerCtor = classDeclaration.Members | ||
.OfType<ConstructorDeclarationSyntax>() | ||
.Select(ctor => ctor.DescendantNodes().OfType<ThrowStatementSyntax>().ToList()) | ||
.Where(@throw => @throw.Count > 0) | ||
.ToList(); | ||
foreach (var throwStatement in throwStatementsPerCtor) | ||
{ | ||
c.ReportDiagnostic(Diagnostic.Create(rule, throwStatement.First().GetLocation(), | ||
throwStatement.Skip(1).Select(@throw => @throw.GetLocation()))); | ||
} | ||
}, | ||
SyntaxKind.ClassDeclaration); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
sonaranalyzer-dotnet/src/SonarAnalyzer.Utilities/Rules.Description/S3693.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<p>It may be a good idea to raise an exception in a constructor if you're unable to fully flesh the object in question, but not in an | ||
<code>exception</code> constructor. If you do, you'll interfere with the exception that was originally being thrown. Further, it is highly unlikely | ||
that an exception raised in the creation of an exception will be properly handled in the calling code, and the unexpected, unhandled exception will | ||
lead to program termination.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
class MyException: Exception | ||
{ | ||
public void MyException() | ||
{ | ||
if (bad_thing) | ||
{ | ||
throw new Exception("A bad thing happened"); // Noncompliant | ||
} | ||
} | ||
} | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...r-dotnet/src/Tests/SonarAnalyzer.UnitTest/Rules/ExceptionConstructorShouldNotThrowTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2017 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using SonarAnalyzer.Rules.CSharp; | ||
|
||
namespace SonarAnalyzer.UnitTest.Rules | ||
{ | ||
[TestClass] | ||
public class ExceptionConstructorShouldNotThrowTest | ||
{ | ||
[TestMethod] | ||
[TestCategory("Rule")] | ||
public void ExceptionConstructorShouldNotThrow() | ||
{ | ||
Verifier.VerifyAnalyzer(@"TestCases\ExceptionConstructorShouldNotThrow.cs", | ||
new ExceptionConstructorShouldNotThrow()); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...r-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/ExceptionConstructorShouldNotThrow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
|
||
namespace Tests.Diagnostics | ||
{ | ||
class MyException : Exception | ||
{ | ||
public MyException() | ||
{ | ||
throw new Exception(); // Noncompliant {{Avoid throwing exceptions in this constructor.}} | ||
// ^^^^^^^^^^^^^^^^^^^^^^ | ||
} | ||
} | ||
|
||
class MyException2 : Exception | ||
{ | ||
public MyException2() | ||
{ | ||
|
||
} | ||
|
||
public MyException2(int i) | ||
{ | ||
if (i == 42) | ||
{ | ||
throw new Exception(); // Noncompliant | ||
} | ||
} | ||
} | ||
|
||
class MyException3 : Exception | ||
{ | ||
public MyException3(int i) | ||
{ | ||
if (i == 42) | ||
{ | ||
throw new Exception(); // Noncompliant | ||
} | ||
else | ||
{ | ||
throw new ArgumentException(); // Secondary | ||
} | ||
} | ||
} | ||
|
||
class SubException : MyException | ||
{ | ||
public SubException() | ||
{ | ||
throw new FieldAccessException(); // Noncompliant | ||
} | ||
} | ||
|
||
class MyException4 : Exception | ||
{ | ||
public MyException4() | ||
{ | ||
throw; // Noncompliant | ||
} | ||
} | ||
|
||
class MyException5 : Exception | ||
{ | ||
public MyException5() | ||
{ | ||
var ex = new Exception(); | ||
throw ex; // Noncompliant | ||
} | ||
} | ||
|
||
class Something | ||
{ | ||
public Something() | ||
{ | ||
throw new Exception(); // Compliant | ||
} | ||
} | ||
} |