Skip to content

Commit cf4c832

Browse files
authored
Merge pull request #2 from leotsarev/exceptions
Exceptions
2 parents c2d5e1e + 3187435 commit cf4c832

35 files changed

+1113
-170
lines changed

HardcodeAnalyzer.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsarev.Analyzer.Hardcode.Em
3737
EndProject
3838
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsarev.Analyzer.Hardcode.Email.Test", "Tsarev.Analyzer.Hardcode.Email.Test\Tsarev.Analyzer.Hardcode.Email.Test.csproj", "{4E74A79F-74BC-49CF-9330-C1EAFCE07E42}"
3939
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsarev.Analyzer.Exceptions", "Tsarev.Analyzer.Exceptions\Tsarev.Analyzer.Exceptions.csproj", "{0F926142-4EEB-4E12-805A-F0963AB2F6BE}"
41+
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tsarev.Analyzer.Exceptions.Test", "Tsarev.Analyzer.Exceptions.Test\Tsarev.Analyzer.Exceptions.Test.csproj", "{F0C90254-A7B4-4CD2-B7D9-CA2B3770FCB2}"
43+
EndProject
4044
Global
4145
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4246
Debug|Any CPU = Debug|Any CPU
@@ -91,6 +95,14 @@ Global
9195
{4E74A79F-74BC-49CF-9330-C1EAFCE07E42}.Debug|Any CPU.Build.0 = Debug|Any CPU
9296
{4E74A79F-74BC-49CF-9330-C1EAFCE07E42}.Release|Any CPU.ActiveCfg = Release|Any CPU
9397
{4E74A79F-74BC-49CF-9330-C1EAFCE07E42}.Release|Any CPU.Build.0 = Release|Any CPU
98+
{0F926142-4EEB-4E12-805A-F0963AB2F6BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
99+
{0F926142-4EEB-4E12-805A-F0963AB2F6BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
100+
{0F926142-4EEB-4E12-805A-F0963AB2F6BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
101+
{0F926142-4EEB-4E12-805A-F0963AB2F6BE}.Release|Any CPU.Build.0 = Release|Any CPU
102+
{F0C90254-A7B4-4CD2-B7D9-CA2B3770FCB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103+
{F0C90254-A7B4-4CD2-B7D9-CA2B3770FCB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
104+
{F0C90254-A7B4-4CD2-B7D9-CA2B3770FCB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
105+
{F0C90254-A7B4-4CD2-B7D9-CA2B3770FCB2}.Release|Any CPU.Build.0 = Release|Any CPU
94106
EndGlobalSection
95107
GlobalSection(SolutionProperties) = preSolution
96108
HideSolutionNode = FALSE
@@ -107,4 +119,7 @@ Global
107119
{F75815ED-B3B6-4A9B-B06E-9625B8F4D13A} = {06D9E499-7167-4C8F-A6EC-3A1390C0F79E}
108120
{4E74A79F-74BC-49CF-9330-C1EAFCE07E42} = {06D9E499-7167-4C8F-A6EC-3A1390C0F79E}
109121
EndGlobalSection
122+
GlobalSection(ExtensibilityGlobals) = postSolution
123+
SolutionGuid = {258BBC45-DDE4-4FD5-A63C-52D794F3BDD3}
124+
EndGlobalSection
110125
EndGlobal
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Diagnostics;
3+
using Tsarev.Analyzer.TestHelpers;
4+
using Xunit;
5+
6+
namespace Tsarev.Analyzer.Exceptions.Test
7+
{
8+
public class ExceptionTest : DiagnosticVerifier
9+
{
10+
[Fact]
11+
public void TestEmpty()
12+
{
13+
var test = @"";
14+
15+
VerifyCSharpDiagnostic(test);
16+
}
17+
18+
[Fact]
19+
public void TestWriteExceptionToLog()
20+
{
21+
var test = @"
22+
class Class {
23+
24+
private void WriteException()
25+
{
26+
var ex = new System.Exception();
27+
Log.Error(ex.Message);
28+
}
29+
}
30+
";
31+
32+
VerifyCSharpDiagnostic(test, Expect(7, 5));
33+
}
34+
35+
[Fact]
36+
public void TestNotException()
37+
{
38+
var test = @"
39+
class Class {
40+
41+
private void WriteException()
42+
{
43+
var ex = new {Message = """"};
44+
Log.Error(ex.Message);
45+
}
46+
}
47+
";
48+
49+
VerifyCSharpDiagnostic(test);
50+
}
51+
52+
[Fact]
53+
public void ConcatExceptionToLog()
54+
{
55+
var test = @"
56+
class Class {
57+
58+
private void WriteException()
59+
{
60+
var ex = new System.Exception();
61+
Log.Error("""" + """" + ex.Message);
62+
}
63+
}
64+
";
65+
66+
VerifyCSharpDiagnostic(test, Expect(7, 5));
67+
}
68+
69+
[Fact]
70+
public void ExceptionPassedToLog()
71+
{
72+
var test = @"
73+
class Class {
74+
75+
private void WriteException()
76+
{
77+
var ex = new System.Exception();
78+
Log.Error("""" + """" + ex.Message, ex);
79+
}
80+
}
81+
";
82+
83+
VerifyCSharpDiagnostic(test);
84+
}
85+
86+
private DiagnosticResult Expect(int line, int column)
87+
=> new DiagnosticResult
88+
{
89+
Id = nameof(ExceptionsAnalyzer),
90+
Message = "Exception data is swalloved. Consider to log exception object instead of just exception.Message",
91+
Severity = DiagnosticSeverity.Warning,
92+
Locations =
93+
new[]
94+
{
95+
new DiagnosticResultLocation("Test0.cs", line, column)
96+
}
97+
};
98+
99+
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() => new ExceptionsAnalyzer();
100+
}
101+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
[assembly: AssemblyTitle("Tsarev.Analyzer.Exceptions.Test")]
5+
[assembly: AssemblyDescription("")]
6+
[assembly: AssemblyConfiguration("")]
7+
[assembly: AssemblyCompany("")]
8+
[assembly: AssemblyProduct("Tsarev.Analyzer.Exceptions.Test")]
9+
[assembly: AssemblyCopyright("Copyright © 2018")]
10+
[assembly: AssemblyTrademark("")]
11+
[assembly: AssemblyCulture("")]
12+
13+
[assembly: ComVisible(false)]
14+
15+
[assembly: Guid("f0c90254-a7b4-4cd2-b7d9-ca2b3770fcb2")]
16+
17+
// [assembly: AssemblyVersion("1.0.*")]
18+
[assembly: AssemblyVersion("1.0.0.0")]
19+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\xunit.core.2.3.1\build\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.3.1\build\xunit.core.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{F0C90254-A7B4-4CD2-B7D9-CA2B3770FCB2}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Tsarev.Analyzer.Exceptions.Test</RootNamespace>
11+
<AssemblyName>Tsarev.Analyzer.Exceptions.Test</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
16+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
17+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
18+
<IsCodedUITest>False</IsCodedUITest>
19+
<TestProjectType>UnitTest</TestProjectType>
20+
<NuGetPackageImportStamp>
21+
</NuGetPackageImportStamp>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
24+
<DebugSymbols>true</DebugSymbols>
25+
<DebugType>full</DebugType>
26+
<Optimize>false</Optimize>
27+
<OutputPath>bin\Debug\</OutputPath>
28+
<DefineConstants>DEBUG;TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
33+
<DebugType>pdbonly</DebugType>
34+
<Optimize>true</Optimize>
35+
<OutputPath>bin\Release\</OutputPath>
36+
<DefineConstants>TRACE</DefineConstants>
37+
<ErrorReport>prompt</ErrorReport>
38+
<WarningLevel>4</WarningLevel>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="Microsoft.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.1.0.1\lib\net45\Microsoft.CodeAnalysis.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.1.0.1\lib\net45\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
46+
</Reference>
47+
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.0.1\lib\net45\Microsoft.CodeAnalysis.CSharp.Workspaces.dll</HintPath>
49+
</Reference>
50+
<Reference Include="Microsoft.CodeAnalysis.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
51+
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.1\lib\net452\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
52+
</Reference>
53+
<Reference Include="Microsoft.CodeAnalysis.Workspaces.Desktop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54+
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.1\lib\net452\Microsoft.CodeAnalysis.Workspaces.Desktop.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System" />
57+
<Reference Include="System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
58+
<HintPath>..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
61+
<Reference Include="System.Composition.AttributedModel, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
62+
<HintPath>..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll</HintPath>
63+
</Reference>
64+
<Reference Include="System.Composition.Convention, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
65+
<HintPath>..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Convention.dll</HintPath>
66+
</Reference>
67+
<Reference Include="System.Composition.Hosting, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
68+
<HintPath>..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Hosting.dll</HintPath>
69+
</Reference>
70+
<Reference Include="System.Composition.Runtime, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
71+
<HintPath>..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Runtime.dll</HintPath>
72+
</Reference>
73+
<Reference Include="System.Composition.TypedParts, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
74+
<HintPath>..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll</HintPath>
75+
</Reference>
76+
<Reference Include="System.Core" />
77+
<Reference Include="System.Reflection.Metadata, Version=1.0.21.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
78+
<HintPath>..\packages\System.Reflection.Metadata.1.0.21\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
79+
</Reference>
80+
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
81+
<HintPath>..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
82+
</Reference>
83+
<Reference Include="xunit.assert, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
84+
<HintPath>..\packages\xunit.assert.2.3.1\lib\netstandard1.1\xunit.assert.dll</HintPath>
85+
</Reference>
86+
<Reference Include="xunit.core, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
87+
<HintPath>..\packages\xunit.extensibility.core.2.3.1\lib\netstandard1.1\xunit.core.dll</HintPath>
88+
</Reference>
89+
<Reference Include="xunit.execution.desktop, Version=2.3.1.3858, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
90+
<HintPath>..\packages\xunit.extensibility.execution.2.3.1\lib\net452\xunit.execution.desktop.dll</HintPath>
91+
</Reference>
92+
</ItemGroup>
93+
<ItemGroup>
94+
<Compile Include="ExceptionTest.cs" />
95+
<Compile Include="Properties\AssemblyInfo.cs" />
96+
</ItemGroup>
97+
<ItemGroup>
98+
<None Include="packages.config" />
99+
</ItemGroup>
100+
<ItemGroup>
101+
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
102+
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
103+
<Analyzer Include="..\packages\xunit.analyzers.0.8.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
104+
</ItemGroup>
105+
<ItemGroup>
106+
<ProjectReference Include="..\Tsarev.Analyzer.Exceptions\Tsarev.Analyzer.Exceptions.csproj">
107+
<Project>{0f926142-4eeb-4e12-805a-f0963ab2f6be}</Project>
108+
<Name>Tsarev.Analyzer.Exceptions</Name>
109+
</ProjectReference>
110+
<ProjectReference Include="..\Tsarev.Analyzer.TestHelpers\Tsarev.Analyzer.TestHelpers.csproj">
111+
<Project>{7ef60ad1-739b-4f59-a4c9-df70be016461}</Project>
112+
<Name>Tsarev.Analyzer.TestHelpers</Name>
113+
</ProjectReference>
114+
</ItemGroup>
115+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
116+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
117+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
118+
<PropertyGroup>
119+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
120+
</PropertyGroup>
121+
<Error Condition="!Exists('..\packages\xunit.core.2.3.1\build\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.3.1\build\xunit.core.props'))" />
122+
<Error Condition="!Exists('..\packages\xunit.core.2.3.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.3.1\build\xunit.core.targets'))" />
123+
</Target>
124+
<Import Project="..\packages\xunit.core.2.3.1\build\xunit.core.targets" Condition="Exists('..\packages\xunit.core.2.3.1\build\xunit.core.targets')" />
125+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" />
4+
<package id="Microsoft.CodeAnalysis.Common" version="1.0.1" targetFramework="net461" />
5+
<package id="Microsoft.CodeAnalysis.CSharp" version="1.0.1" targetFramework="net461" />
6+
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.1" targetFramework="net461" />
7+
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.1" targetFramework="net461" />
8+
<package id="Microsoft.Composition" version="1.0.27" targetFramework="net461" />
9+
<package id="System.Collections.Immutable" version="1.1.36" targetFramework="net461" />
10+
<package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net461" />
11+
<package id="xunit" version="2.3.1" targetFramework="net461" />
12+
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
13+
<package id="xunit.analyzers" version="0.8.0" targetFramework="net461" />
14+
<package id="xunit.assert" version="2.3.1" targetFramework="net461" />
15+
<package id="xunit.core" version="2.3.1" targetFramework="net461" />
16+
<package id="xunit.extensibility.core" version="2.3.1" targetFramework="net461" />
17+
<package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net461" />
18+
</packages>

0 commit comments

Comments
 (0)