Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullReferenceException when NoWarn is empty #77

Merged
merged 3 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public AnnotatorBuildTask()
TargetFrameworkDirectories = null!;
AnnotatedReferenceAssemblyDirectory = null!;
OutputPath = null!;
DisabledWarnings = null!;
}

[Required]
Expand Down Expand Up @@ -50,7 +49,7 @@ public string OutputPath
set;
}

public string DisabledWarnings
public string? DisabledWarnings
{
get;
set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal readonly struct SuppressibleLoggingHelper
private readonly ImmutableHashSet<string> _disabledWarnings;
private readonly string _requiredPrefix;

public SuppressibleLoggingHelper(TaskLoggingHelper helper, string requiredPrefix, string disabledWarnings)
public SuppressibleLoggingHelper(TaskLoggingHelper helper, string requiredPrefix, string? disabledWarnings)
{
_helper = helper;

Expand All @@ -24,10 +24,11 @@ public SuppressibleLoggingHelper(TaskLoggingHelper helper, string requiredPrefix
_requiredPrefix = requiredPrefix;

_disabledWarnings = disabledWarnings
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
?.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(item => item.Trim())
.Where(item => item.StartsWith(requiredPrefix, StringComparison.Ordinal))
.ToImmutableHashSet(StringComparer.Ordinal);
.ToImmutableHashSet(StringComparer.Ordinal)
?? ImmutableHashSet<string>.Empty;
}

public void LogWarning(string warningCode, string message, params object?[] messageArgs)
Expand Down
5 changes: 5 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ test_script:
- git clean -dxf tests
- msbuild /restore tests/RefVersion3.1.0 /warnaserror /nr:false /v:m
- git clean -dxf tests
# No /warnaserror because WarningsNotAsErrors is not respected
- msbuild /restore tests/NoDisabledWarnings /p:TreatWarningsAsErrors=true /nr:false /v:m
- git clean -dxf tests
- msbuild /restore tests/SingleTFM /p:TestFramework=net35 /warnaserror /nr:false /v:m
- git clean -dxf tests
- msbuild /restore tests/SingleTFM /p:TestFramework=net48 /warnaserror /nr:false /v:m
Expand Down Expand Up @@ -44,6 +47,8 @@ test_script:
- git clean -dxf tests
- dotnet msbuild -restore tests/RefVersion3.1.0 -warnaserror -nr:false -v:m
- git clean -dxf tests
- dotnet msbuild -restore tests/NoDisabledWarnings -warnaserror -nr:false -v:m
- git clean -dxf tests
- dotnet msbuild -restore tests/SingleTFM -p:TestFramework=net35 -warnaserror -nr:false -v:m
- git clean -dxf tests
- dotnet msbuild -restore tests/SingleTFM -p:TestFramework=net48 -warnaserror -nr:false -v:m
Expand Down
19 changes: 19 additions & 0 deletions tests/NoDisabledWarnings/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;

namespace Sample1
{
public class Class1
{
public static void M(SqlConnection connection, [AllowNull] string? x)
{
if (!string.IsNullOrEmpty(x))
{
x.ToString();
}

_ = EqualityComparer<string?>.Default.GetHashCode(x);
}
}
}
32 changes: 32 additions & 0 deletions tests/NoDisabledWarnings/NoDisabledWarnings.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net35;net48;netstandard1.6;netstandard2.0;netstandard2.1;netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;netcoreapp3.1</TargetFrameworks>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<WarningsNotAsErrors>RA1000</WarningsNotAsErrors>
<NoWarn></NoWarn>
</PropertyGroup>

<PropertyGroup>
<!-- NuGet cannot restore packages for netcoreapp1.0: https://github.com/NuGet/Home/issues/7414 -->
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp1.0' OR '$(TargetFramework)' == 'netcoreapp1.1'">
<!-- CS1701: Assuming assembly reference "Assembly Name #1" matches "Assembly Name #2", you may need to supply runtime policy -->
<WarningsNotAsErrors>$(WarningsNotAsErrors),CS1701</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
<!-- https://github.com/dotnet/core-sdk/issues/2022 -->
<PackageReference Condition="'$(TargetFramework)' == 'net35'" Include="jnm2.ReferenceAssemblies.net35" Version="1.0.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Condition="'$(TargetFramework)' != 'net35'" Include="System.Data.SqlClient" Version="4.8.0" />
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.*" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[3.0.0]" />
</ItemGroup>

</Project>