Skip to content
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 @@ -68,7 +68,7 @@ private void AnalyzeNamespace(SyntaxNodeAnalysisContext context)
diagnosticLocation,
severity,
ImmutableArray.Create(declaration.GetLocation()),
ImmutableDictionary<string, string>.Empty);
ImmutableDictionary<string, string?>.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void AnalyzeNamespace(SyntaxNodeAnalysisContext context)
diagnosticLocation,
severity,
ImmutableArray.Create(declaration.GetLocation()),
ImmutableDictionary<string, string>.Empty);
ImmutableDictionary<string, string?>.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace Microsoft.CodeAnalysis.CSharp.UseIsNullCheck
internal class CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer
: AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
private static readonly ImmutableDictionary<string, string> s_properties =
ImmutableDictionary<string, string>.Empty.Add(UseIsNullConstants.Kind, UseIsNullConstants.CastAndEqualityKey);
private static readonly ImmutableDictionary<string, string?> s_properties =
ImmutableDictionary<string, string?>.Empty.Add(UseIsNullConstants.Kind, UseIsNullConstants.CastAndEqualityKey);

public CSharpUseIsNullCheckForCastAndEqualityOperatorDiagnosticAnalyzer()
: base(IDEDiagnosticIds.UseIsNullCheckDiagnosticId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal sealed class CSharpUsePatternCombinatorsDiagnosticAnalyzer :
private static readonly LocalizableResourceString s_safePatternTitle = new(nameof(CSharpAnalyzersResources.Use_pattern_matching), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources));
private static readonly LocalizableResourceString s_unsafePatternTitle = new(nameof(CSharpAnalyzersResources.Use_pattern_matching_may_change_code_meaning), CSharpAnalyzersResources.ResourceManager, typeof(CSharpAnalyzersResources));

private static readonly ImmutableDictionary<string, string> s_safeProperties = ImmutableDictionary<string, string>.Empty.Add(SafeKey, "");
private static readonly ImmutableDictionary<string, string?> s_safeProperties = ImmutableDictionary<string, string?>.Empty.Add(SafeKey, "");
private static readonly DiagnosticDescriptor s_unsafeDescriptor = CreateDescriptorWithId(
IDEDiagnosticIds.UsePatternCombinatorsDiagnosticId,
EnforceOnBuildValues.UsePatternCombinators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeStyle;
Expand Down Expand Up @@ -55,7 +53,7 @@ protected static DiagnosticDescriptor CreateDescriptor(
bool isUnnecessary,
bool isEnabledByDefault = true,
bool isConfigurable = true,
LocalizableString description = null)
LocalizableString? description = null)
#pragma warning disable RS0030 // Do not used banned APIs
=> new(
id, title, messageFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal abstract class AbstractAddRequiredParenthesesDiagnosticAnalyzer<
where TBinaryLikeExpressionSyntax : TExpressionSyntax
where TLanguageKindEnum : struct
{
private static readonly Dictionary<(bool includeInFixAll, string equivalenceKey), ImmutableDictionary<string, string>> s_cachedProperties =
private static readonly Dictionary<(bool includeInFixAll, string equivalenceKey), ImmutableDictionary<string, string?>> s_cachedProperties =
new();

private readonly IPrecedenceService _precedenceService;
Expand All @@ -38,7 +38,7 @@ static AbstractAddRequiredParenthesesDiagnosticAnalyzer()
{
foreach (var includeInFixAll in includeArray)
{
var properties = ImmutableDictionary<string, string>.Empty;
var properties = ImmutableDictionary<string, string?>.Empty;
if (includeInFixAll)
{
properties = properties.Add(AddRequiredParenthesesConstants.IncludeInFixAll, "");
Expand All @@ -54,7 +54,7 @@ static AbstractAddRequiredParenthesesDiagnosticAnalyzer()
private static string GetEquivalenceKey(PerLanguageOption2<CodeStyleOption2<ParenthesesPreference>> parentPrecedence)
=> parentPrecedence.Name;

private static ImmutableDictionary<string, string> GetProperties(bool includeInFixAll, string equivalenceKey)
private static ImmutableDictionary<string, string?> GetProperties(bool includeInFixAll, string equivalenceKey)
=> s_cachedProperties[(includeInFixAll, equivalenceKey)];

protected abstract int GetPrecedence(TBinaryLikeExpressionSyntax binaryLike);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

namespace Microsoft.CodeAnalysis.AddRequiredParentheses
{
internal static class AddRequiredParenthesesConstants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.Shared.Utilities
{
internal readonly struct DeserializationConstructorCheck
{
private readonly INamedTypeSymbol _iSerializableType;
private readonly INamedTypeSymbol _serializationInfoType;
private readonly INamedTypeSymbol _streamingContextType;
private readonly INamedTypeSymbol? _iSerializableType;
private readonly INamedTypeSymbol? _serializationInfoType;
private readonly INamedTypeSymbol? _streamingContextType;

public DeserializationConstructorCheck(Compilation compilation)
{
Expand Down
30 changes: 14 additions & 16 deletions src/Analyzers/Core/Analyzers/Helpers/DiagnosticHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -41,8 +39,8 @@ public static Diagnostic Create(
DiagnosticDescriptor descriptor,
Location location,
ReportDiagnostic effectiveSeverity,
IEnumerable<Location> additionalLocations,
ImmutableDictionary<string, string> properties,
IEnumerable<Location>? additionalLocations,
ImmutableDictionary<string, string?>? properties,
params object[] messageArgs)
{
if (descriptor == null)
Expand Down Expand Up @@ -92,7 +90,7 @@ public static Diagnostic CreateWithLocationTags(
{
if (additionalUnnecessaryLocations.IsEmpty)
{
return Create(descriptor, location, effectiveSeverity, additionalLocations, ImmutableDictionary<string, string>.Empty, messageArgs);
return Create(descriptor, location, effectiveSeverity, additionalLocations, ImmutableDictionary<string, string?>.Empty, messageArgs);
}

var tagIndices = ImmutableDictionary<string, IEnumerable<int>>.Empty
Expand All @@ -103,7 +101,7 @@ public static Diagnostic CreateWithLocationTags(
effectiveSeverity,
additionalLocations.AddRange(additionalUnnecessaryLocations),
tagIndices,
ImmutableDictionary<string, string>.Empty,
ImmutableDictionary<string, string?>.Empty,
messageArgs);
}

Expand Down Expand Up @@ -136,12 +134,12 @@ public static Diagnostic CreateWithLocationTags(
ReportDiagnostic effectiveSeverity,
ImmutableArray<Location> additionalLocations,
ImmutableArray<Location> additionalUnnecessaryLocations,
ImmutableDictionary<string, string> properties,
ImmutableDictionary<string, string?> properties,
params object[] messageArgs)
{
if (additionalUnnecessaryLocations.IsEmpty)
{
return Create(descriptor, location, effectiveSeverity, additionalLocations, ImmutableDictionary<string, string>.Empty, messageArgs);
return Create(descriptor, location, effectiveSeverity, additionalLocations, ImmutableDictionary<string, string?>.Empty, messageArgs);
}

var tagIndices = ImmutableDictionary<string, IEnumerable<int>>.Empty
Expand Down Expand Up @@ -182,14 +180,14 @@ private static Diagnostic CreateWithLocationTags(
ReportDiagnostic effectiveSeverity,
IEnumerable<Location> additionalLocations,
IDictionary<string, IEnumerable<int>> tagIndices,
ImmutableDictionary<string, string> properties,
ImmutableDictionary<string, string?> properties,
params object[] messageArgs)
{
Contract.ThrowIfTrue(additionalLocations.IsEmpty());
Contract.ThrowIfTrue(tagIndices.IsEmpty());

properties ??= ImmutableDictionary<string, string>.Empty;
properties = properties.AddRange(tagIndices.Select(kvp => new KeyValuePair<string, string>(kvp.Key, EncodeIndices(kvp.Value, additionalLocations.Count()))));
properties ??= ImmutableDictionary<string, string?>.Empty;
properties = properties.AddRange(tagIndices.Select(kvp => new KeyValuePair<string, string?>(kvp.Key, EncodeIndices(kvp.Value, additionalLocations.Count()))));

return Create(descriptor, location, effectiveSeverity, additionalLocations, properties, messageArgs);

Expand Down Expand Up @@ -230,8 +228,8 @@ public static Diagnostic CreateWithMessage(
DiagnosticDescriptor descriptor,
Location location,
ReportDiagnostic effectiveSeverity,
IEnumerable<Location> additionalLocations,
ImmutableDictionary<string, string> properties,
IEnumerable<Location>? additionalLocations,
ImmutableDictionary<string, string?>? properties,
LocalizableString message)
{
if (descriptor == null)
Expand All @@ -257,7 +255,7 @@ public static Diagnostic CreateWithMessage(
properties);
}

public static string GetHelpLinkForDiagnosticId(string id)
public static string? GetHelpLinkForDiagnosticId(string id)
{
if (id == "RE0001")
{
Expand Down Expand Up @@ -332,15 +330,15 @@ void IObjectWritable.WriteTo(ObjectWriter writer)
}
}

protected override string GetText(IFormatProvider formatProvider)
protected override string GetText(IFormatProvider? formatProvider)
{
var messageFormat = _messageFormat.ToString(formatProvider);
return messageFormat != null ?
(_formatArguments.Length > 0 ? string.Format(formatProvider, messageFormat, _formatArguments) : messageFormat) :
string.Empty;
}

protected override bool AreEqual(object other)
protected override bool AreEqual(object? other)
{
return other is LocalizableStringWithArguments otherResourceString &&
_messageFormat.Equals(otherResourceString._messageFormat) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.Options;

Expand All @@ -23,7 +22,7 @@ internal static class IDEDiagnosticIdToOptionMappingHelper
private static readonly ConcurrentDictionary<string, ImmutableHashSet<IOption2>> s_diagnosticIdToOptionMap = new();
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, ImmutableHashSet<IOption2>>> s_diagnosticIdToLanguageSpecificOptionsMap = new();

public static bool TryGetMappedOptions(string diagnosticId, string language, out ImmutableHashSet<IOption2> options)
public static bool TryGetMappedOptions(string diagnosticId, string language, [NotNullWhen(true)] out ImmutableHashSet<IOption2>? options)
=> s_diagnosticIdToOptionMap.TryGetValue(diagnosticId, out options) ||
(s_diagnosticIdToLanguageSpecificOptionsMap.TryGetValue(language, out var map) &&
map.TryGetValue(diagnosticId, out options));
Expand Down
2 changes: 0 additions & 2 deletions src/Analyzers/Core/Analyzers/IDEDiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using Microsoft.CodeAnalysis.Formatting;

namespace Microsoft.CodeAnalysis.Diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
return null;
}

var builder = ImmutableDictionary.CreateBuilder<string, string>();
var builder = ImmutableDictionary.CreateBuilder<string, string?>();
builder[nameof(NamingStyle)] = applicableRule.NamingStyle.CreateXElement().ToString();
builder["OptionName"] = nameof(NamingStyleOptions.NamingPreferences);
builder["OptionLanguage"] = compilation.Language;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Generic;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Microsoft.CodeAnalysis.OrderModifiers
Expand All @@ -17,7 +16,7 @@ internal abstract class AbstractOrderModifiersHelpers
/// <remarks>
/// Reference type so we can read/write atomically.
/// </remarks>
private Tuple<string, Dictionary<int, int>> _lastParsed;
private Tuple<string, Dictionary<int, int>>? _lastParsed;

protected abstract int GetKeywordKind(string trimmed);

Expand All @@ -41,7 +40,7 @@ public static bool IsOrdered(Dictionary<int, int> preferredOrder, SyntaxTokenLis
return true;
}

public bool TryGetOrComputePreferredOrder(string value, out Dictionary<int, int> preferredOrder)
public bool TryGetOrComputePreferredOrder(string value, [NotNullWhen(true)] out Dictionary<int, int>? preferredOrder)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand All @@ -66,7 +65,7 @@ public bool TryGetOrComputePreferredOrder(string value, out Dictionary<int, int>
return true;
}

protected virtual bool TryParse(string value, out Dictionary<int, int> parsed)
protected virtual bool TryParse(string value, [NotNullWhen(true)] out Dictionary<int, int>? parsed)
{
var result = new Dictionary<int, int>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -87,51 +85,37 @@ private void AnalyzeOperation(OperationAnalysisContext context)
}
}

private void AnalyzeOperation(OperationAnalysisContext context, IOperation operation, IOperation instanceOperation)
private void AnalyzeOperation(OperationAnalysisContext context, IOperation operation, IOperation? instanceOperation)
{
// this is a static reference so we don't care if it's qualified
if (instanceOperation == null)
{
return;
}

// if we're not referencing `this.` or `Me.` (e.g., a parameter, local, etc.)
if (instanceOperation.Kind != OperationKind.InstanceReference)
{
return;
}

// We shouldn't qualify if it is inside a property pattern
if (context.Operation.Parent.Kind == OperationKind.PropertySubpattern)
{
if (context.Operation.Parent?.Kind == OperationKind.PropertySubpattern)
return;
}

// Initializer lists are IInvocationOperation which if passed to GetApplicableOptionFromSymbolKind
// will incorrectly fetch the options for method call.
// We still want to handle InstanceReferenceKind.ContainingTypeInstance
if ((instanceOperation as IInstanceReferenceOperation)?.ReferenceKind == InstanceReferenceKind.ImplicitReceiver)
{
return;
}

// If we can't be qualified (e.g., because we're already qualified with `base.`), we're done.
if (!CanMemberAccessBeQualified(context.ContainingSymbol, instanceOperation.Syntax))
{
return;
}

// if we can't find a member then we can't do anything. Also, we shouldn't qualify
// accesses to static members.
if (IsStaticMemberOrIsLocalFunction(operation))
{
return;
}

if (instanceOperation.Syntax is not TSimpleNameSyntax simpleName)
{
return;
}

var applicableOption = QualifyMembersHelpers.GetApplicableOptionFromSymbolKind(operation);
var optionValue = context.GetOption(applicableOption, context.Operation.Syntax.Language);
Expand Down
Loading