Skip to content

Use auto properties #75936

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 15 additions & 21 deletions src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,50 @@
{
private static readonly string s_enforceOnBuildNeverTag = EnforceOnBuild.Never.ToCustomTag();

private static readonly string[] s_microsoftCustomTags = [WellKnownDiagnosticTags.Telemetry];
private static readonly string[] s_editAndContinueCustomTags = [WellKnownDiagnosticTags.EditAndContinue, WellKnownDiagnosticTags.Telemetry, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag];
private static readonly string[] s_unnecessaryCustomTags = [WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.Telemetry];
private static readonly string[] s_notConfigurableCustomTags = [WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry];
private static readonly string[] s_unnecessaryAndNotConfigurableCustomTags = [WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry];

public static string[] Microsoft

Check failure on line 17 in src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs#L17

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs(17,28): error CS8050: (NETCORE_ENGINEERING_TELEMETRY=Build) Only auto-implemented properties can have initializers.
{
get
{
Assert(s_microsoftCustomTags, WellKnownDiagnosticTags.Telemetry);
return s_microsoftCustomTags;
Assert(field, WellKnownDiagnosticTags.Telemetry);
return field;
}
}
} = [WellKnownDiagnosticTags.Telemetry];

public static string[] EditAndContinue

Check failure on line 26 in src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs#L26

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs(26,28): error CS8050: (NETCORE_ENGINEERING_TELEMETRY=Build) Only auto-implemented properties can have initializers.
{
get
{
Assert(s_editAndContinueCustomTags, WellKnownDiagnosticTags.EditAndContinue, WellKnownDiagnosticTags.Telemetry, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag);
return s_editAndContinueCustomTags;
Assert(field, WellKnownDiagnosticTags.EditAndContinue, WellKnownDiagnosticTags.Telemetry, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag);
return field;
}
}
} = [WellKnownDiagnosticTags.EditAndContinue, WellKnownDiagnosticTags.Telemetry, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag];

public static string[] Unnecessary

Check failure on line 35 in src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs#L35

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs(35,28): error CS8050: (NETCORE_ENGINEERING_TELEMETRY=Build) Only auto-implemented properties can have initializers.
{
get
{
Assert(s_unnecessaryCustomTags, WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.Telemetry);
return s_unnecessaryCustomTags;
Assert(field, WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.Telemetry);
return field;
}
}
} = [WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.Telemetry];

public static string[] NotConfigurable

Check failure on line 44 in src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs#L44

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs(44,28): error CS8050: (NETCORE_ENGINEERING_TELEMETRY=Build) Only auto-implemented properties can have initializers.
{
get
{
Assert(s_notConfigurableCustomTags, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry);
return s_notConfigurableCustomTags;
Assert(field, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry);
return field;
}
}
} = [WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry];

public static string[] UnnecessaryAndNotConfigurable

Check failure on line 53 in src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs#L53

src/Analyzers/Core/Analyzers/DiagnosticCustomTags.cs(53,28): error CS8050: (NETCORE_ENGINEERING_TELEMETRY=Build) Only auto-implemented properties can have initializers.
{
get
{
Assert(s_unnecessaryAndNotConfigurableCustomTags, WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry);
return s_unnecessaryAndNotConfigurableCustomTags;
Assert(field, WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry);
return field;
}
}
} = [WellKnownDiagnosticTags.Unnecessary, WellKnownDiagnosticTags.NotConfigurable, s_enforceOnBuildNeverTag, WellKnownDiagnosticTags.Telemetry];

[Conditional("DEBUG")]
private static void Assert(string[] customTags, params string[] tags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.Undo;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text;
Expand Down
8 changes: 2 additions & 6 deletions src/EditorFeatures/Core/InlineRename/InlineRenameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ internal partial class InlineRenameSession : IInlineRenameSession, IFeatureContr

private bool _dismissed;
private bool _isApplyingEdit;
private string _replacementText;
private readonly Dictionary<ITextBuffer, OpenTextBufferManager> _openTextBuffers = [];

/// <summary>
Expand All @@ -74,13 +73,10 @@ internal partial class InlineRenameSession : IInlineRenameSession, IFeatureContr
/// </summary>
public string ReplacementText
{
get
{
return _replacementText;
}
get;
private set
{
_replacementText = value;
field = value;
ReplacementTextChanged?.Invoke(this, EventArgs.Empty);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/EditorFeatures/Core/IntelliSense/ModelComputation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ internal class ModelComputation<TModel> where TModel : class
#region Fields that can only be accessed from the foreground thread

private readonly IController<TModel> _controller;
private readonly TaskScheduler __taskScheduler;

private TaskScheduler _taskScheduler
{
get
{
ThreadingContext.ThrowIfNotOnUIThread();
return __taskScheduler;
return field;
}
}

Expand All @@ -62,7 +61,7 @@ public ModelComputation(
{
ThreadingContext = threadingContext;
_controller = controller;
__taskScheduler = computationTaskScheduler;
_taskScheduler = computationTaskScheduler;

_stopTokenSource = new CancellationTokenSource();
_stopCancellationToken = _stopTokenSource.Token;
Expand Down
14 changes: 4 additions & 10 deletions src/Features/Core/Portable/Completion/CompletionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public sealed class CompletionContext
{
private readonly SegmentedList<CompletionItem> _items = [];

private CompletionItem? _suggestionModeItem;
private bool _isExclusive;

internal CompletionProvider Provider { get; }

/// <summary>
Expand Down Expand Up @@ -89,15 +86,15 @@ public bool IsExclusive
{
get
{
return _isExclusive && !Provider.IsExpandItemProvider;
return field && !Provider.IsExpandItemProvider;
}

set
{
if (value)
Debug.Assert(!Provider.IsExpandItemProvider);

_isExclusive = value;
field = value;
}
}

Expand Down Expand Up @@ -198,10 +195,7 @@ public void AddItems(IEnumerable<CompletionItem> items)
/// </summary>
public CompletionItem? SuggestionModeItem
{
get
{
return _suggestionModeItem;
}
get;

set
{
Expand All @@ -210,7 +204,7 @@ public CompletionItem? SuggestionModeItem
value = FixItem(value);
}

_suggestionModeItem = value;
field = value;
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/Features/Core/Portable/Completion/CompletionDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.CodeAnalysis.Completion;
/// </summary>
public sealed class CompletionDescription
{
private string? _text;

/// <summary>
/// The <see cref="CompletionDescription"/> used when there is no description.
/// </summary>
Expand Down Expand Up @@ -63,12 +61,12 @@ public string Text
{
get
{
if (_text == null)
if (field == null)
{
Interlocked.CompareExchange(ref _text, string.Concat(TaggedParts.Select(p => p.Text)), null);
Interlocked.CompareExchange(ref field, string.Concat(TaggedParts.Select(p => p.Text)), null);
}

return _text;
return field;
}
}
} = null!;
}
12 changes: 5 additions & 7 deletions src/Features/Core/Portable/QuickInfo/QuickInfoSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,26 @@ private QuickInfoSection(string? kind, ImmutableArray<TaggedText> taggedParts)
public static QuickInfoSection Create(string? kind, ImmutableArray<TaggedText> taggedParts)
=> new(kind, taggedParts);

private string? _text;

/// <summary>
/// The text of the section without tags.
/// </summary>
public string Text
{
get
{
if (_text == null)
if (field == null)
{
if (TaggedParts.Length == 0)
{
_text = string.Empty;
field = string.Empty;
}
else
{
Interlocked.CompareExchange(ref _text, string.Concat(TaggedParts.Select(t => t.Text)), null);
Interlocked.CompareExchange(ref field, string.Concat(TaggedParts.Select(t => t.Text)), null);
}
}

return _text;
return field;
}
}
} = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ public AddParameterDialogViewModel(Document document, int positionForTypeBinding

public bool TypeBinds => !TypeSymbol!.IsErrorType();

private bool _isRequired;
public bool IsRequired
{
get => _isRequired;
get;
set
{
if (SetProperty(ref _isRequired, value))
if (SetProperty(ref field, value))
{
NotifyPropertyChanged(nameof(IsOptional));

Expand All @@ -83,10 +82,10 @@ public bool IsRequired

public bool IsOptional
{
get => !_isRequired;
get => !IsRequired;
set
{
if (_isRequired == value)
if (IsRequired == value)
{
IsRequired = !value;
}
Expand All @@ -106,19 +105,17 @@ public bool IsOptional
public Visibility TypeDoesNotBindImage { get; set; }
public Visibility TypeDoesNotParseOrInvalidTypeImage { get; set; }
public Visibility TypeIsEmptyImage { get; set; }

private string _verbatimTypeName = string.Empty;
public string VerbatimTypeName
{
get => _verbatimTypeName;
get;
set
{
if (SetProperty(ref _verbatimTypeName, value))
if (SetProperty(ref field, value))
{
SetCurrentTypeTextAndUpdateBindingStatus(value);
}
}
}
} = string.Empty;

private bool _isVoidParameterType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ private bool EditableParameterSelected(out int index)

internal void Remove()
{
if (AllParameters[_selectedIndex!.Value] is AddedParameterViewModel)
if (AllParameters[SelectedIndex!.Value] is AddedParameterViewModel)
{
var parameterToRemove = AllParameters[_selectedIndex!.Value];
var parameterToRemove = AllParameters[SelectedIndex!.Value];

if (!_parametersWithoutDefaultValues.Remove(parameterToRemove))
{
Expand All @@ -236,7 +236,7 @@ internal void Remove()
}
else
{
AllParameters[_selectedIndex!.Value].IsRemoved = true;
AllParameters[SelectedIndex!.Value].IsRemoved = true;
}

UpdateNameConflictMarkers();
Expand All @@ -245,7 +245,7 @@ internal void Remove()

internal void Restore()
{
AllParameters[_selectedIndex!.Value].IsRemoved = false;
AllParameters[SelectedIndex!.Value].IsRemoved = false;
UpdateNameConflictMarkers();
RemoveRestoreNotifyPropertyChanged();
}
Expand Down Expand Up @@ -507,23 +507,19 @@ private bool IsDisabled(ParameterViewModel parameterViewModel)
return _disabledParameters.Contains(parameterViewModel);
}

private int? _selectedIndex;
public int? SelectedIndex
{
get
{
return _selectedIndex;
}
get;

set
{
var newSelectedIndex = value == -1 ? null : value;
if (newSelectedIndex == _selectedIndex)
if (newSelectedIndex == field)
{
return;
}

_selectedIndex = newSelectedIndex;
field = newSelectedIndex;

NotifyPropertyChanged(nameof(CanMoveUp));
NotifyPropertyChanged(nameof(MoveUpAutomationText));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ public MemberSelectionViewModel(

public bool ShowCheckDependentsButton { get; }
public bool ShowPublicButton { get; }
public bool ShowMakeAbstract => _members.Any(m => m.IsMakeAbstractCheckable);
public bool ShowMakeAbstract => Members.Any(m => m.IsMakeAbstractCheckable);
public ImmutableArray<MemberSymbolViewModel> CheckedMembers => Members.WhereAsArray(m => m.IsChecked && m.IsCheckable);

private ImmutableArray<MemberSymbolViewModel> _members;
public ImmutableArray<MemberSymbolViewModel> Members
{
get => _members;
get;
set
{
var oldMembers = _members;
if (SetProperty(ref _members, value))
var oldMembers = field;
if (SetProperty(ref field, value))
{
// If we have registered for events before, remove the handlers
// to be a good citizen in the world
Expand All @@ -68,7 +67,7 @@ public ImmutableArray<MemberSymbolViewModel> Members
}
}

foreach (var member in _members)
foreach (var member in field)
{
member.PropertyChanged += MemberPropertyChangedHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,17 @@ public string FileName
set { SetProperty(ref _fileName, value); }
}

private NewTypeDestination _destination = NewTypeDestination.NewFile;
public NewTypeDestination Destination
{
get { return _destination; }
get;
set
{
if (SetProperty(ref _destination, value))
if (SetProperty(ref field, value))
{
NotifyPropertyChanged(nameof(FileNameEnabled));
}
}
}
} = NewTypeDestination.NewFile;

internal bool TrySubmit([NotNullWhen(returnValue: false)] out string? message)
{
Expand Down
Loading
Loading