Skip to content

Commit

Permalink
Add single-file analyzer and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
agocke committed Aug 14, 2021
1 parent 65e5065 commit c032e9a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
7 changes: 6 additions & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<CoverletOutput>$(TargetDir)coverage\$(MSBuildProjectName).coverage</CoverletOutput>
</PropertyGroup>

<!-- Turn on single-file publishing analysis for everything except the test projects -->
<PropertyGroup Condition="'$(IsTestProject)' != 'true'">
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
</PropertyGroup>

<PropertyGroup Condition="'$(IsTestProject)' == 'true' ">
<TargetArchitecture Condition="'$(TargetArchitecture)' == ''">x64</TargetArchitecture>
<Platform Condition="'$(Platform)' == 'AnyCPU' or '$(Platform)' == 'Any CPU'">$(TargetArchitecture)</Platform>
Expand Down Expand Up @@ -52,7 +57,7 @@

<ItemGroup>
<!-- Set TargetingPackVersion -->
<FrameworkReference Update="Microsoft.NETCore.App"
<FrameworkReference Update="Microsoft.NETCore.App"
Condition=" '$(VSRedistCommonNetCoreSharedFrameworkx6460PackageVersion)'!='' And $(TargetFramework.StartsWith('net6.')) ">
<TargetingPackVersion>$(VSRedistCommonNetCoreSharedFrameworkx6460PackageVersion)</TargetingPackVersion>
</FrameworkReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.Serialization;
using System.Windows.Forms;
Expand Down Expand Up @@ -556,6 +557,7 @@ public Type GetType(IDesignerHost host)
/// locate the type. If reference is true, the given assembly name will be added
/// to the designer host's set of references.
/// </summary>
[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Single-file case is handled")]
protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference)
{
ITypeResolutionService ts = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Collections;
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace System.Resources
Expand All @@ -29,6 +30,7 @@ public Assembly GetAssembly(AssemblyName name)
return GetAssembly(name, true);
}

[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Handles single file case")]
public Assembly GetAssembly(AssemblyName name, bool throwOnError)
{
Assembly result = null;
Expand Down Expand Up @@ -76,6 +78,7 @@ public Assembly GetAssembly(AssemblyName name, bool throwOnError)
return result;
}

[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Returns null if in a single file")]
public string GetPathOfAssembly(AssemblyName name)
{
return name.CodeBase;
Expand Down
23 changes: 4 additions & 19 deletions src/System.Windows.Forms/src/System/Windows/Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.Reflection;
Expand Down Expand Up @@ -55,9 +56,6 @@ public sealed partial class Application
private static readonly object s_eventApplicationExit = new object();
private static readonly object s_eventThreadExit = new object();

// Constant string used in Application.Restart()
private const string IEEXEC = "ieexec.exe";

// Defines a new callback delegate type
[EditorBrowsable(EditorBrowsableState.Advanced)]
public delegate bool MessageLoopCallback();
Expand Down Expand Up @@ -819,6 +817,7 @@ internal static void DoEventsModal()
/// Enables visual styles for all subsequent <see cref="Run()"/> and <see cref="Control.CreateHandle"/> calls.
/// Uses the default theming manifest file shipped with the redist.
/// </summary>
[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Single-file case is handled")]
public static void EnableVisualStyles()
{
// Pull manifest from our resources
Expand Down Expand Up @@ -951,14 +950,15 @@ internal static void FormActivated(bool modal, bool activated)
/// Retrieves the FileVersionInfo associated with the main module for
/// the application.
/// </summary>
[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Single-file case is handled")]
private static FileVersionInfo GetAppFileVersionInfo()
{
lock (s_internalSyncObject)
{
if (s_appFileVersion is null)
{
Type t = GetAppMainType();
if (t is not null)
if (t is not null && t.Assembly.Location.Length > 0)
{
s_appFileVersion = FileVersionInfo.GetVersionInfo(t.Module.FullyQualifiedName);
}
Expand Down Expand Up @@ -1140,21 +1140,6 @@ public static void Restart()

Process process = Process.GetCurrentProcess();
Debug.Assert(process is not null);
if (string.Equals(process.MainModule.ModuleName, IEEXEC, StringComparison.OrdinalIgnoreCase))
{
string clrPath = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

if (string.Equals(clrPath + "\\" + IEEXEC, process.MainModule.FileName, StringComparison.OrdinalIgnoreCase))
{
// HRef exe case
hrefExeCase = true;
Exit();
if (AppDomain.CurrentDomain.GetData("APP_LAUNCH_URL") is string launchUrl)
{
Process.Start(process.MainModule.FileName, launchUrl);
}
}
}

if (!hrefExeCase)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace System.Windows.Forms
Expand All @@ -25,6 +26,7 @@ internal ControlVersionInfo(Control owner)
/// <summary>
/// The company name associated with the component.
/// </summary>
[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Single-file case is handled")]
internal string CompanyName
{
get
Expand All @@ -37,7 +39,7 @@ internal string CompanyName
_companyName = ((AssemblyCompanyAttribute)attrs[0]).Company;
}

if (_companyName is null || _companyName.Length == 0)
if ((_companyName is null || _companyName.Length == 0) && !OwnerIsInMemoryAssembly)
{
_companyName = GetFileVersionInfo().CompanyName;
if (_companyName is not null)
Expand Down Expand Up @@ -74,6 +76,7 @@ internal string CompanyName
/// <summary>
/// The product name associated with this component.
/// </summary>
[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Single-file case is handled")]
internal string ProductName
{
get
Expand All @@ -86,7 +89,7 @@ internal string ProductName
_productName = ((AssemblyProductAttribute)attrs[0]).Product;
}

if (_productName is null || _productName.Length == 0)
if ((_productName is null || _productName.Length == 0) && !OwnerIsInMemoryAssembly)
{
_productName = GetFileVersionInfo().ProductName;
if (_productName is not null)
Expand Down Expand Up @@ -123,6 +126,7 @@ internal string ProductName
/// <summary>
/// The product version associated with this component.
/// </summary>
[UnconditionalSuppressMessage("SingleFile", "IL3002", Justification = "Single-file case is handled")]
internal string ProductVersion
{
get
Expand All @@ -137,7 +141,7 @@ internal string ProductVersion
}

// win32 version info
if (_productVersion is null || _productVersion.Length == 0)
if ((_productVersion is null || _productVersion.Length == 0) && !OwnerIsInMemoryAssembly)
{
_productVersion = GetFileVersionInfo().ProductVersion;
if (_productVersion is not null)
Expand All @@ -161,6 +165,7 @@ internal string ProductVersion
/// Retrieves the FileVersionInfo associated with the main module for
/// the component.
/// </summary>
[RequiresAssemblyFiles("Throws if _owner is an in-memory assembly. Check " + nameof(OwnerIsInMemoryAssembly) + " first")]
private FileVersionInfo GetFileVersionInfo()
{
if (_versionInfo is null)
Expand All @@ -172,6 +177,9 @@ private FileVersionInfo GetFileVersionInfo()

return _versionInfo;
}

//[UnconditionalSuppressMessage("SingleFile", "IL3000")]
private bool OwnerIsInMemoryAssembly => _owner.GetType().Assembly.Location.Length == 0;
}
}
}

0 comments on commit c032e9a

Please sign in to comment.