Skip to content

Commit

Permalink
Suppress trimming warnings in source-generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrozsival committed Jan 29, 2024
1 parent bd67960 commit abb35ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Controls/src/Build.Tasks/XamlCTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ public override bool Execute(out IList<Exception> thrownExceptions)
LoggingHelper.LogMessage(Low, e.StackTrace);
continue;
}
if (initComp.HasCustomAttributes)
{
var suppressMessageAttribute = initComp.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute");
if (suppressMessageAttribute != null)
{
LoggingHelper.LogMessage(Low, $"{new string(' ', 6)}Removing UnconditionalSuppressMessageAttribute from InitializeComponent()");
initComp.CustomAttributes.Remove(suppressMessageAttribute);
}
}
if (Type != null)
InitCompForType = initComp;

Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/SourceGen/CodeBehindGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ static void GenerateXamlCodeBehind(ProjectItem projItem, Compilation compilation

//initializeComponent
sb.AppendLine($"\t\t[global::System.CodeDom.Compiler.GeneratedCode(\"Microsoft.Maui.Controls.SourceGen\", \"1.0.0.0\")]");
sb.AppendLine($"\t\t[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(\"Trimming\", \"IL2026\", Justification = \"Source-generated code will be replaced by XamlC in Release builds.\")]");

// add MemberNotNull attributes
if (namedFields != null)
Expand Down

0 comments on commit abb35ee

Please sign in to comment.