Skip to content

Commit

Permalink
Finish enabling unnecessary assignment analyzer (#12534)
Browse files Browse the repository at this point in the history
Now is enabled as a warning again and will fail the command line build.
Removes last TODOs from .editorconfig
  • Loading branch information
JeremyKuhne authored Nov 23, 2024
1 parent a4daa07 commit cac5a6c
Show file tree
Hide file tree
Showing 82 changed files with 406 additions and 557 deletions.
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ dotnet_diagnostic.IDE0057.severity = suggestion
dotnet_diagnostic.IDE0058.severity = silent

# IDE0059: Unnecessary assignment of a value
dotnet_diagnostic.IDE0059.severity = suggestion # TODO: warning
dotnet_diagnostic.IDE0059.severity = warning

# IDE0060: Remove unused parameter
dotnet_diagnostic.IDE0060.severity = silent
Expand Down Expand Up @@ -708,7 +708,7 @@ dotnet_diagnostic.CA1819.severity = none
dotnet_diagnostic.CA1820.severity = none

# CA1821: Remove empty Finalizers
dotnet_diagnostic.CA1821.severity = none # TODO: warning
dotnet_diagnostic.CA1821.severity = warning

# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = warning
Expand Down Expand Up @@ -1523,8 +1523,8 @@ dotnet_diagnostic.SA1204.severity = none
# SA1205: Partial elements should declare an access modifier
dotnet_diagnostic.SA1205.severity = warning

# SA1206: Keyword ordering - TODO Re-enable as warning after https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3527
dotnet_diagnostic.SA1206.severity = suggestion
# SA1206: Keyword ordering
dotnet_diagnostic.SA1206.severity = warning

# SA1207: Protected should come before internal
dotnet_diagnostic.SA1207.severity = none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Namespace Microsoft.VisualBasic.Forms.Tests
Public Sub CreateTempDirectoryTest()
Dim tempDirectory As String = CreateTempDirectory()
tempDirectory.Should.StartWith(Path.GetTempPath)
tempDirectory = CreateTempDirectory()
CreateTempDirectory()
_testDirectories.Count.Should.Be(1)
tempDirectory = CreateTempDirectory(lineNumber:=1)
CreateTempDirectory(lineNumber:=1)
_testDirectories.Count.Should.Be(2)
End Sub

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public void Ctor_Default_Success()
public void CanDuplex_ReturnsExpected()
{
PrinterSettings printerSettings = new();
bool canDuplex = printerSettings.CanDuplex;
_ = printerSettings.CanDuplex;
}

[Fact]
public void Copies_Default_ReturnsExpected()
{
PrinterSettings printerSettings = new();
int copies = printerSettings.Copies;
_ = printerSettings.Copies;
}

[Theory]
Expand Down Expand Up @@ -79,7 +79,7 @@ public void Copies_SetValue_ThrowsArgumentException(short copies)
public void Collate_Default_ReturnsExpected()
{
PrinterSettings printerSettings = new();
bool collate = printerSettings.Collate;
_ = printerSettings.Collate;
}

[Fact]
Expand Down Expand Up @@ -380,7 +380,7 @@ public static IEnumerable<object[]> IsDirectPrintingSupported_ImageFormatSupport
public void IsDirectPrintingSupported_ImageFormatSupported_ReturnsExpected(ImageFormat imageFormat)
{
PrinterSettings printerSettings = new();
bool supported = printerSettings.IsDirectPrintingSupported(imageFormat);
_ = printerSettings.IsDirectPrintingSupported(imageFormat);
}

public static IEnumerable<object[]> IsDirectPrintingSupported_ImageFormatNotSupported_TestData()
Expand Down Expand Up @@ -508,21 +508,17 @@ public void CreateMeasurementGraphics_Null_ThrowsNullReferenceException()
public void GetHdevmode_ReturnsExpected()
{
PrinterSettings printerSettings = new();
IntPtr handle = IntPtr.Zero;

handle = printerSettings.GetHdevmode();
Assert.NotEqual(IntPtr.Zero, handle);
nint handle = printerSettings.GetHdevmode();
Assert.NotEqual(0, handle);
}

[Fact]
public void GetHdevmode_PageSettings_ReturnsExpected()
{
PrinterSettings printerSettings = new();
PageSettings pageSettings = new();
IntPtr handle = IntPtr.Zero;

handle = printerSettings.GetHdevmode(pageSettings);
Assert.NotEqual(IntPtr.Zero, handle);
nint handle = printerSettings.GetHdevmode(pageSettings);
Assert.NotEqual(0, handle);
}

[Fact]
Expand All @@ -536,10 +532,8 @@ public void GetHdevmode_Null_ThrowsNullReferenceException()
public void GetHdevnames_ReturnsExpected()
{
PrinterSettings printerSettings = new();
IntPtr handle = IntPtr.Zero;

handle = printerSettings.GetHdevnames();
Assert.NotEqual(IntPtr.Zero, handle);
nint handle = printerSettings.GetHdevnames();
Assert.NotEqual(0, handle);
}

[ConditionalFact(typeof(PrinterSettingsTests), nameof(CanTestSetHdevmode_IntPtr_Success))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void CheckForEmptyBitmap(Bitmap bitmap)

private static void CheckForNonEmptyBitmap(Bitmap bitmap)
{
if (IsEmptyBitmap(bitmap, out int x, out int y))
if (IsEmptyBitmap(bitmap, out int _, out int _))
Assert.True(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,49 +293,49 @@ public virtual CodeStatementCollection SerializeMemberAbsolute(IDesignerSerializ
[Obsolete("This method has been deprecated. Use SerializeToExpression or GetExpression instead. https://go.microsoft.com/fwlink/?linkid=14202")]
protected CodeExpression? SerializeToReferenceExpression(IDesignerSerializationManager manager, object value)
{
CodeExpression? expression = null;

// First - try GetExpression
expression = GetExpression(manager, value);
if (GetExpression(manager, value) is { } expression)
{
return expression;
}

if (value is not IComponent)
{
return null;
}

// Next, we check for a named IComponent, and return a reference to it.
if (expression is null && value is IComponent)
string? name = manager.GetName(value);
bool referenceName = false;
if (name is null)
{
string? name = manager.GetName(value);
bool referenceName = false;
if (name is null)
IReferenceService? referenceService = manager.GetService<IReferenceService>();
if (referenceService is not null)
{
IReferenceService? referenceService = manager.GetService<IReferenceService>();
if (referenceService is not null)
{
name = referenceService.GetName(value);
referenceName = name is not null;
}
name = referenceService.GetName(value);
referenceName = name is not null;
}
}

if (name is not null)
{
// Check to see if this is a reference to the root component. If it is, then use "this".
if (manager.TryGetContext(out RootContext? root) && root.Value == value)
{
expression = root.Expression;
}
else
{
int dotIndex = name.IndexOf('.');
if (referenceName && dotIndex != -1)
{
// if it's a reference name with a dot, we've actually got a property here...
expression = new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(s_thisRef, name[..dotIndex]), name[(dotIndex + 1)..]);
}
else
{
expression = new CodeFieldReferenceExpression(s_thisRef, name);
}
}
}
if (name is null)
{
return null;
}

// Check to see if this is a reference to the root component. If it is, then use "this".
if (manager.TryGetContext(out RootContext? root) && root.Value == value)
{
return root.Expression;
}

return expression;
// If it's a reference name with a dot, we've actually got a property.

int dotIndex = name.IndexOf('.');
return referenceName && dotIndex != -1
? new CodePropertyReferenceExpression(
new CodeFieldReferenceExpression(s_thisRef, name[..dotIndex]),
name[(dotIndex + 1)..])
: new CodeFieldReferenceExpression(s_thisRef, name);
}

private static void ResetBrowsableProperties(object? instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public IDisposable CreateSession()
// Check for a default serialization provider
if (_defaultProviderTable is null || !_defaultProviderTable.Contains(serializerType))
{
Type? defaultSerializerType = null;
Type? defaultSerializerType;
DefaultSerializationProviderAttribute? attribute = (DefaultSerializationProviderAttribute?)TypeDescriptor.GetAttributes(serializerType)[typeof(DefaultSerializationProviderAttribute)];
if (attribute is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public override object Serialize(IDesignerSerializationManager manager, object?
return null!;
}

bool needCast = false;
bool needCast;
Enum[] values;
TypeConverter? converter = TypeDescriptor.GetConverter(enumValue);
if (converter is not null && converter.CanConvertTo(typeof(Enum[])))
{
values = (Enum[])converter.ConvertTo(enumValue, typeof(Enum[]))!;
needCast = (values.Length > 1);
needCast = values.Length > 1;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public virtual object Deserialize(IDesignerSerializationManager manager, CodeTyp
/// </summary>
private object? DeserializeName(IDesignerSerializationManager manager, string name, CodeStatementCollection? statements)
{
object? value = null;
object? value;

// If the name we're looking for isn't in our dictionary, we return null.
// It is up to the caller to decide if this is an error or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected static string CreateFilterEntry(ImageEditor e)

public override object? EditValue(ITypeDescriptorContext? context, IServiceProvider provider, object? value)
{
if (!provider.TryGetService(out IWindowsFormsEditorService? editorService))
if (!provider.TryGetService(out IWindowsFormsEditorService? _))
{
return value;
}
Expand All @@ -86,6 +86,7 @@ protected static string CreateFilterEntry(ImageEditor e)
binder: null,
args: null,
culture: null);

if (editor is not null
&& editor.GetType() is Type editorClass
&& !myClass.Equals(editorClass)
Expand Down Expand Up @@ -118,7 +119,6 @@ protected static string CreateFilterEntry(ImageEditor e)
return value;
}

/// <inheritdoc />
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) => UITypeEditorEditStyle.Modal;

protected virtual string GetFileDialogDescription() => SR.imageFileDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ private void SetBitmap(string fileName)
private void CollapseGlyph(Rectangle bounds)
{
DockStyle? dock = _relatedPanel?.Dock;
int x = 0;
int y = 0;
int x;
int y;

switch (dock)
{
Expand Down Expand Up @@ -147,8 +147,8 @@ private void CollapseGlyph(Rectangle bounds)
private void ExpandGlyph(Rectangle bounds)
{
DockStyle? dock = _relatedPanel?.Dock;
int x = 0;
int y = 0;
int x;
int y;

switch (dock)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,45 +334,58 @@ private static Type[] ToTargetTypes(object context, Type[] runtimeTypes)
/// <summary>
/// Serializes a method invocation on the control being serialized. Used to serialize Suspend/ResumeLayout pairs, etc.
/// </summary>
private void SerializeMethodInvocation(IDesignerSerializationManager manager, CodeStatementCollection statements, object control, string methodName, CodeExpressionCollection? parameters, Type[] paramTypes, StatementOrdering ordering)
private void SerializeMethodInvocation(
IDesignerSerializationManager manager,
CodeStatementCollection statements,
object control,
string methodName,
CodeExpressionCollection? parameters,
Type[] paramTypes,
StatementOrdering ordering)
{
string? name = manager.GetName(control);
// Not doing anything with the name, keeping the access for compat.
_ = manager.GetName(control);

// Use IReflect to see if this method name exists on the control.
paramTypes = ToTargetTypes(control, paramTypes);
MethodInfo? mi = TypeDescriptor.GetReflectionType(control).GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, binder: null, paramTypes, modifiers: null);

if (mi is not null)
if (TypeDescriptor.GetReflectionType(control).GetMethod(
methodName,
BindingFlags.Public | BindingFlags.Instance,
binder: null,
paramTypes,
modifiers: null) is null)
{
CodeExpression? field = SerializeToExpression(manager, control);
CodeMethodReferenceExpression method = new(field, methodName);
CodeMethodInvokeExpression methodInvoke = new()
{
Method = method
};
return;
}

if (parameters is not null)
{
methodInvoke.Parameters.AddRange(parameters);
}
CodeExpression? field = SerializeToExpression(manager, control);
CodeMethodReferenceExpression method = new(field, methodName);
CodeMethodInvokeExpression methodInvoke = new()
{
Method = method
};

CodeExpressionStatement statement = new(methodInvoke);
if (parameters is not null)
{
methodInvoke.Parameters.AddRange(parameters);
}

switch (ordering)
{
case StatementOrdering.Prepend:
statement.UserData["statement-ordering"] = "begin";
break;
case StatementOrdering.Append:
statement.UserData["statement-ordering"] = "end";
break;
default:
Debug.Fail($"Unsupported statement ordering: {ordering}");
break;
}
CodeExpressionStatement statement = new(methodInvoke);

statements.Add(statement);
switch (ordering)
{
case StatementOrdering.Prepend:
statement.UserData["statement-ordering"] = "begin";
break;
case StatementOrdering.Append:
statement.UserData["statement-ordering"] = "end";
break;
default:
Debug.Fail($"Unsupported statement ordering: {ordering}");
break;
}

statements.Add(statement);
}

private void SerializePerformLayout(IDesignerSerializationManager manager, CodeStatementCollection statements, object control)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ protected override void PreFilterProperties(IDictionary properties)
}

/// <summary>
/// Hooks the children of the given control. We need to do this for child controls that are not in design
/// Unhooks the children of the given control. We need to do this for child controls that are not in design
/// mode, which is the case for composite controls.
/// </summary>
protected void UnhookChildControls(Control firstChild)
Expand All @@ -1674,11 +1674,9 @@ protected void UnhookChildControls(Control firstChild)

foreach (Control child in firstChild.Controls)
{
IWindowTarget? oldTarget = null;
if (child is not null)
{
// No, no designer means we must replace the window target in this control.
oldTarget = child.WindowTarget;
IWindowTarget? oldTarget = child.WindowTarget;
if (oldTarget is ChildWindowTarget target)
{
child.WindowTarget = target.OldWindowTarget;
Expand Down
Loading

0 comments on commit cac5a6c

Please sign in to comment.