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

[Controls] Enforce CA1507 - use nameof() where possible #22102

Merged
merged 2 commits into from May 20, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Controls/src/Core/.editorconfig
@@ -0,0 +1,2 @@
[*.cs]
dotnet_diagnostic.CA1507.severity = error
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Maui.Controls
public partial class ActivityIndicator : View, IColorElement, IElementConfiguration<ActivityIndicator>, IActivityIndicator
{
/// <summary>Bindable property for <see cref="IsRunning"/>.</summary>
public static readonly BindableProperty IsRunningProperty = BindableProperty.Create("IsRunning", typeof(bool), typeof(ActivityIndicator), default(bool));
public static readonly BindableProperty IsRunningProperty = BindableProperty.Create(nameof(IsRunning), typeof(bool), typeof(ActivityIndicator), default(bool));

/// <summary>Bindable property for <see cref="Color"/>.</summary>
public static readonly BindableProperty ColorProperty = ColorElement.ColorProperty;
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/Animation.cs
Expand Up @@ -45,10 +45,10 @@ public Animation(Action<double> callback, double start = 0.0f, double end = 1.0f
public void Add(double beginAt, double finishAt, Animation animation)
{
if (beginAt < 0 || beginAt > 1)
throw new ArgumentOutOfRangeException("beginAt");
throw new ArgumentOutOfRangeException(nameof(beginAt));

if (finishAt < 0 || finishAt > 1)
throw new ArgumentOutOfRangeException("finishAt");
throw new ArgumentOutOfRangeException(nameof(finishAt));

if (finishAt <= beginAt)
throw new ArgumentException("finishAt must be greater than beginAt");
Expand Down
18 changes: 9 additions & 9 deletions src/Controls/src/Core/Cells/Cell.cs
Expand Up @@ -16,7 +16,7 @@ public abstract class Cell : Element, ICellController, IFlowDirectionController,
/// <include file="../../../docs/Microsoft.Maui.Controls/Cell.xml" path="//Member[@MemberName='DefaultCellHeight']/Docs/*" />
public const int DefaultCellHeight = 40;
/// <summary>Bindable property for <see cref="IsEnabled"/>.</summary>
public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create("IsEnabled", typeof(bool), typeof(Cell), true, propertyChanged: OnIsEnabledPropertyChanged);
public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(Cell), true, propertyChanged: OnIsEnabledPropertyChanged);

ObservableCollection<MenuItem> _contextActions;
List<MenuItem> _currentContextActions;
Expand Down Expand Up @@ -116,11 +116,11 @@ public double Height
if (_height == value)
return;

OnPropertyChanging("Height");
OnPropertyChanging("RenderHeight");
OnPropertyChanging(nameof(Height));
OnPropertyChanging(nameof(RenderHeight));
_height = value;
OnPropertyChanged("Height");
OnPropertyChanged("RenderHeight");
OnPropertyChanged(nameof(Height));
OnPropertyChanged(nameof(RenderHeight));
}
}

Expand Down Expand Up @@ -265,7 +265,7 @@ void OnContextActionsChanged(object sender, NotifyCollectionChangedEventArgs e)

_currentContextActions = new List<MenuItem>(_contextActions);

OnPropertyChanged("HasContextActions");
OnPropertyChanged(nameof(HasContextActions));
}

async void OnForceUpdateSizeRequested()
Expand All @@ -279,15 +279,15 @@ async void OnForceUpdateSizeRequested()

static void OnIsEnabledPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
{
(bindable as Cell).OnPropertyChanged("HasContextActions");
(bindable as Cell).OnPropertyChanged(nameof(HasContextActions));
}

void OnParentPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Technically we might be raising this even if it didn't change, but I'm taking the bet that
// its uncommon enough that we don't want to take the penalty of N GetValue calls to verify.
if (e.PropertyName == "RowHeight")
OnPropertyChanged("RenderHeight");
OnPropertyChanged(nameof(RenderHeight));
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName ||
e.PropertyName == VisualElement.VisualProperty.PropertyName)
PropertyPropagationController.PropagatePropertyChanged(e.PropertyName);
Expand All @@ -296,7 +296,7 @@ void OnParentPropertyChanged(object sender, PropertyChangedEventArgs e)
void OnParentPropertyChanging(object sender, PropertyChangingEventArgs e)
{
if (e.PropertyName == "RowHeight")
OnPropertyChanging("RenderHeight");
OnPropertyChanging(nameof(RenderHeight));
}

#if ANDROID
Expand Down
10 changes: 5 additions & 5 deletions src/Controls/src/Core/Cells/EntryCell.cs
Expand Up @@ -9,19 +9,19 @@ namespace Microsoft.Maui.Controls
public class EntryCell : Cell, ITextAlignmentElement, IEntryCellController, ITextAlignment
{
/// <summary>Bindable property for <see cref="Text"/>.</summary>
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(EntryCell), null, BindingMode.TwoWay);
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(EntryCell), null, BindingMode.TwoWay);

/// <summary>Bindable property for <see cref="Label"/>.</summary>
public static readonly BindableProperty LabelProperty = BindableProperty.Create("Label", typeof(string), typeof(EntryCell), null);
public static readonly BindableProperty LabelProperty = BindableProperty.Create(nameof(Label), typeof(string), typeof(EntryCell), null);

/// <summary>Bindable property for <see cref="Placeholder"/>.</summary>
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create("Placeholder", typeof(string), typeof(EntryCell), null);
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(EntryCell), null);

/// <summary>Bindable property for <see cref="LabelColor"/>.</summary>
public static readonly BindableProperty LabelColorProperty = BindableProperty.Create("LabelColor", typeof(Color), typeof(EntryCell), null);
public static readonly BindableProperty LabelColorProperty = BindableProperty.Create(nameof(LabelColor), typeof(Color), typeof(EntryCell), null);

/// <summary>Bindable property for <see cref="Keyboard"/>.</summary>
public static readonly BindableProperty KeyboardProperty = BindableProperty.Create("Keyboard", typeof(Keyboard), typeof(EntryCell), Keyboard.Default);
public static readonly BindableProperty KeyboardProperty = BindableProperty.Create(nameof(Keyboard), typeof(Keyboard), typeof(EntryCell), Keyboard.Default);

/// <summary>Bindable property for <see cref="HorizontalTextAlignment"/>.</summary>
public static readonly BindableProperty HorizontalTextAlignmentProperty = TextAlignmentElement.HorizontalTextAlignmentProperty;
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Cells/ImageCell.cs
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Maui.Controls
public class ImageCell : TextCell
{
/// <summary>Bindable property for <see cref="ImageSource"/>.</summary>
public static readonly BindableProperty ImageSourceProperty = BindableProperty.Create("ImageSource", typeof(ImageSource), typeof(ImageCell), null,
public static readonly BindableProperty ImageSourceProperty = BindableProperty.Create(nameof(ImageSource), typeof(ImageSource), typeof(ImageCell), null,
propertyChanging: (bindable, oldvalue, newvalue) => ((ImageCell)bindable).OnSourcePropertyChanging((ImageSource)oldvalue, (ImageSource)newvalue),
propertyChanged: (bindable, oldvalue, newvalue) => ((ImageCell)bindable).OnSourcePropertyChanged((ImageSource)oldvalue, (ImageSource)newvalue));

Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/Cells/SwitchCell.cs
Expand Up @@ -8,14 +8,14 @@ namespace Microsoft.Maui.Controls
public class SwitchCell : Cell
{
/// <summary>Bindable property for <see cref="On"/>.</summary>
public static readonly BindableProperty OnProperty = BindableProperty.Create("On", typeof(bool), typeof(SwitchCell), false, propertyChanged: (obj, oldValue, newValue) =>
public static readonly BindableProperty OnProperty = BindableProperty.Create(nameof(On), typeof(bool), typeof(SwitchCell), false, propertyChanged: (obj, oldValue, newValue) =>
{
var switchCell = (SwitchCell)obj;
switchCell.OnChanged?.Invoke(obj, new ToggledEventArgs((bool)newValue));
}, defaultBindingMode: BindingMode.TwoWay);

/// <summary>Bindable property for <see cref="Text"/>.</summary>
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SwitchCell), default(string));
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(SwitchCell), default(string));

/// <summary>Bindable property for <see cref="OnColor"/>.</summary>
public static readonly BindableProperty OnColorProperty = BindableProperty.Create(nameof(OnColor), typeof(Color), typeof(SwitchCell), null);
Expand Down
12 changes: 6 additions & 6 deletions src/Controls/src/Core/Cells/TextCell.cs
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Maui.Controls
public class TextCell : Cell
{
/// <summary>Bindable property for <see cref="Command"/>.</summary>
public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(TextCell), default(ICommand),
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(TextCell), default(ICommand),
propertyChanging: (bindable, oldvalue, newvalue) =>
{
var textCell = (TextCell)bindable;
Expand All @@ -28,7 +28,7 @@ public class TextCell : Cell
});

/// <summary>Bindable property for <see cref="CommandParameter"/>.</summary>
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(TextCell), default(object),
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(TextCell), default(object),
propertyChanged: (bindable, oldvalue, newvalue) =>
{
var textCell = (TextCell)bindable;
Expand All @@ -39,16 +39,16 @@ public class TextCell : Cell
});

/// <summary>Bindable property for <see cref="Text"/>.</summary>
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(TextCell), default(string));
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(TextCell), default(string));

/// <summary>Bindable property for <see cref="Detail"/>.</summary>
public static readonly BindableProperty DetailProperty = BindableProperty.Create("Detail", typeof(string), typeof(TextCell), default(string));
public static readonly BindableProperty DetailProperty = BindableProperty.Create(nameof(Detail), typeof(string), typeof(TextCell), default(string));

/// <summary>Bindable property for <see cref="TextColor"/>.</summary>
public static readonly BindableProperty TextColorProperty = BindableProperty.Create("TextColor", typeof(Color), typeof(TextCell), null);
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(TextCell), null);

/// <summary>Bindable property for <see cref="DetailColor"/>.</summary>
public static readonly BindableProperty DetailColorProperty = BindableProperty.Create("DetailColor", typeof(Color), typeof(TextCell), null);
public static readonly BindableProperty DetailColorProperty = BindableProperty.Create(nameof(DetailColor), typeof(Color), typeof(TextCell), null);

/// <include file="../../../docs/Microsoft.Maui.Controls/TextCell.xml" path="//Member[@MemberName='Command']/Docs/*" />
public ICommand Command
Expand Down
Expand Up @@ -69,7 +69,7 @@ public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSi
case NamedSize.Title:
return 24;
default:
throw new ArgumentOutOfRangeException("size");
throw new ArgumentOutOfRangeException(nameof(size));
}
}
switch (size)
Expand Down Expand Up @@ -101,7 +101,7 @@ public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSi
case NamedSize.Title:
return 24;
default:
throw new ArgumentOutOfRangeException("size");
throw new ArgumentOutOfRangeException(nameof(size));
}
}

Expand Down
Expand Up @@ -32,7 +32,7 @@ public abstract class CellAdapter : BaseAdapter<object>, AdapterView.IOnItemLong
protected CellAdapter(Context context)
{
if (context == null)
throw new ArgumentNullException("context");
throw new ArgumentNullException(nameof(context));

_context = context;
}
Expand Down
Expand Up @@ -599,9 +599,9 @@ void UpdateTranslucent()
void InsertPageBefore(Page page, Page before)
{
if (before.Handler is not IPlatformViewHandler nvh)
throw new ArgumentNullException("before");
throw new ArgumentNullException(nameof(before));
if (page == null)
throw new ArgumentNullException("page");
throw new ArgumentNullException(nameof(page));

var pageContainer = CreateViewControllerForPage(page);
var target = nvh.ViewController.ParentViewController;
Expand Down Expand Up @@ -641,7 +641,7 @@ void OnRemovedPageRequested(object sender, NavigationRequestedEventArgs e)
void RemovePage(Page page)
{
if (page?.Handler is not IPlatformViewHandler nvh)
throw new ArgumentNullException("page");
throw new ArgumentNullException(nameof(page));
if (page == Current)
throw new NotSupportedException(); // should never happen as NavPage protects against this

Expand Down
Expand Up @@ -10,7 +10,7 @@ internal static class CellExtensions
internal static NSIndexPath GetIndexPath(this Cell self)
{
if (self == null)
throw new ArgumentNullException("self");
throw new ArgumentNullException(nameof(self));

NSIndexPath path;

Expand Down
Expand Up @@ -72,7 +72,7 @@ public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSi

#endif
default:
throw new ArgumentOutOfRangeException("size");
throw new ArgumentOutOfRangeException(nameof(size));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/ElementTemplate.cs
Expand Up @@ -25,15 +25,15 @@ internal ElementTemplate()
: this()
{
if (type == null)
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));

_canRecycle = true;
_type = type;

LoadTemplate = () => Activator.CreateInstance(type);
}

internal ElementTemplate(Func<object> loadTemplate) : this() => LoadTemplate = loadTemplate ?? throw new ArgumentNullException("loadTemplate");
internal ElementTemplate(Func<object> loadTemplate) : this() => LoadTemplate = loadTemplate ?? throw new ArgumentNullException(nameof(loadTemplate));

public Func<object> LoadTemplate { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/FileImageSource.cs
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Maui.Controls
public sealed partial class FileImageSource : ImageSource
{
/// <summary>Bindable property for <see cref="File"/>.</summary>
public static readonly BindableProperty FileProperty = BindableProperty.Create("File", typeof(string), typeof(FileImageSource), default(string));
public static readonly BindableProperty FileProperty = BindableProperty.Create(nameof(File), typeof(string), typeof(FileImageSource), default(string));

/// <include file="../../docs/Microsoft.Maui.Controls/FileImageSource.xml" path="//Member[@MemberName='IsEmpty']/Docs/*" />
public override bool IsEmpty => string.IsNullOrEmpty(File);
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/FlyoutPage/FlyoutPage.cs
Expand Up @@ -41,7 +41,7 @@ public Page Detail
set
{
if (_detail != null && value == null)
throw new ArgumentNullException("value", "Detail cannot be set to null once a value is set.");
throw new ArgumentNullException(nameof(value), "Detail cannot be set to null once a value is set.");

if (_detail == value)
return;
Expand Down Expand Up @@ -92,7 +92,7 @@ public Page Flyout
set
{
if (_flyout != null && value == null)
throw new ArgumentNullException("value", "Flyout cannot be set to null once a value is set");
throw new ArgumentNullException(nameof(value), "Flyout cannot be set to null once a value is set");

if (string.IsNullOrEmpty(value.Title))
throw new InvalidOperationException("Title property must be set on Flyout page");
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/FocusEventArgs.cs
Expand Up @@ -10,7 +10,7 @@ public class FocusEventArgs : EventArgs
public FocusEventArgs(VisualElement visualElement, bool isFocused)
{
if (visualElement == null)
throw new ArgumentNullException("visualElement");
throw new ArgumentNullException(nameof(visualElement));

VisualElement = visualElement;
IsFocused = isFocused;
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/FontImageSource.cs
Expand Up @@ -56,7 +56,7 @@ public double Size

/// <summary>Bindable property for <see cref="FontAutoScalingEnabled"/>.</summary>
public static readonly BindableProperty FontAutoScalingEnabledProperty =
BindableProperty.Create("FontAutoScalingEnabled", typeof(bool), typeof(FontImageSource), false,
BindableProperty.Create(nameof(FontAutoScalingEnabled), typeof(bool), typeof(FontImageSource), false,
propertyChanged: (b, o, n) => ((FontImageSource)b).OnSourceChanged());

public bool FontAutoScalingEnabled
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Frame/Frame.cs
Expand Up @@ -13,7 +13,7 @@ public partial class Frame : ContentView, IElementConfiguration<Frame>, IPadding
public static readonly BindableProperty BorderColorProperty = BorderElement.BorderColorProperty;

/// <summary>Bindable property for <see cref="HasShadow"/>.</summary>
public static readonly BindableProperty HasShadowProperty = BindableProperty.Create("HasShadow", typeof(bool), typeof(Frame), true);
public static readonly BindableProperty HasShadowProperty = BindableProperty.Create(nameof(HasShadow), typeof(bool), typeof(Frame), true);

/// <summary>Bindable property for <see cref="CornerRadius"/>.</summary>
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(Frame), -1.0f,
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/HtmlWebViewSource.cs
Expand Up @@ -7,11 +7,11 @@ namespace Microsoft.Maui.Controls
public class HtmlWebViewSource : WebViewSource
{
/// <summary>Bindable property for <see cref="Html"/>.</summary>
public static readonly BindableProperty HtmlProperty = BindableProperty.Create("Html", typeof(string), typeof(HtmlWebViewSource), default(string),
public static readonly BindableProperty HtmlProperty = BindableProperty.Create(nameof(Html), typeof(string), typeof(HtmlWebViewSource), default(string),
propertyChanged: (bindable, oldvalue, newvalue) => ((HtmlWebViewSource)bindable).OnSourceChanged());

/// <summary>Bindable property for <see cref="BaseUrl"/>.</summary>
public static readonly BindableProperty BaseUrlProperty = BindableProperty.Create("BaseUrl", typeof(string), typeof(HtmlWebViewSource), default(string),
public static readonly BindableProperty BaseUrlProperty = BindableProperty.Create(nameof(BaseUrl), typeof(string), typeof(HtmlWebViewSource), default(string),
propertyChanged: (bindable, oldvalue, newvalue) => ((HtmlWebViewSource)bindable).OnSourceChanged());

/// <include file="../../docs/Microsoft.Maui.Controls/HtmlWebViewSource.xml" path="//Member[@MemberName='BaseUrl']/Docs/*" />
Expand Down
Expand Up @@ -24,7 +24,7 @@ public AttachedCollection(IList<T> list) : base(list)
public void AttachTo(BindableObject bindable)
{
if (bindable == null)
throw new ArgumentNullException("bindable");
throw new ArgumentNullException(nameof(bindable));
OnAttachedTo(bindable);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Interactivity/TriggerAction.cs
Expand Up @@ -8,7 +8,7 @@ public abstract class TriggerAction
internal TriggerAction(Type associatedType)
{
if (associatedType == null)
throw new ArgumentNullException("associatedType");
throw new ArgumentNullException(nameof(associatedType));
AssociatedType = associatedType;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Core/Interactivity/TriggerBase.cs
Expand Up @@ -67,7 +67,7 @@ void IAttachedObject.AttachTo(BindableObject bindable)
IsSealed = true;

if (bindable == null)
throw new ArgumentNullException("bindable");
throw new ArgumentNullException(nameof(bindable));
if (!TargetType.IsInstanceOfType(bindable))
throw new InvalidOperationException("bindable not an instance of AssociatedType");
OnAttachedTo(bindable);
Expand All @@ -76,7 +76,7 @@ void IAttachedObject.AttachTo(BindableObject bindable)
void IAttachedObject.DetachFrom(BindableObject bindable)
{
if (bindable == null)
throw new ArgumentNullException("bindable");
throw new ArgumentNullException(nameof(bindable));
OnDetachingFrom(bindable);
}

Expand Down