Skip to content

Instantly share code, notes, and snippets.

@badmotorfinger
Last active June 15, 2017 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badmotorfinger/ffff7ced5d0ed04cd896be47291a5ed9 to your computer and use it in GitHub Desktop.
Save badmotorfinger/ffff7ced5d0ed04cd896be47291a5ed9 to your computer and use it in GitHub Desktop.
Recursively processes all csproj files in a directory and adds/updates a node for both Release and Debug configurations to treat the specified C# warnings as errors
# Warnings to treat as errors
# See end of script for descriptions
$cSharpWarnings = '162,168,169,219,1717,0067,649,618,183,184,1060,1058,809,672,612,1522,465,1998'
$formatting = ',1570,1574'
$interopWarnings = ',626,684,824'
$casWarnings = ',688'
$warnings = $cSharpWarnings + $formatting + $interopWarnings + $casWarnings
Get-ChildItem *.csproj -Recurse | % {
$xml = [xml](Get-Content $_)
$namespace = $xml.Project.GetAttribute("xmlns")
Write-Host "Processing file" $_.FullName
$xml.Project.PropertyGroup | ? {
$_.Condition -like '*release*' -or $_.Condition -like '*debug*'
} | % {
$warnAsErrorNode = $_.Item("WarningsAsErrors")
if ($warnAsErrorNode -eq $null) {
$warnAsErrorNode = $xml.CreateElement("WarningsAsErrors", $namespace)
$_.AppendChild($warnAsErrorNode);
}
$warnAsErrorNode.InnerText = $warnings;
}
$xml.Save($_.FullName);
}
# CS0162 - The compiler detected code that will never be executed.
# https://msdn.microsoft.com/en-us/library/c0h4st1x(v=vs.90).aspx
# CS0168 - The compiler warns when a variable is declared but not used.
# https://msdn.microsoft.com/en-us/library/tf85t6e4(v=vs.90).aspx
# CS0169 - A private variable was declared but never referenced
# https://msdn.microsoft.com/en-us/library/x7sk421w(v=vs.90).aspx
# CS0219 - The compiler issues a level-three warning, when you declare and assign a variable, but do not use it.
# https://msdn.microsoft.com/en-us/library/4kycwe2x.aspx
# CS1717 - This warning occurs when you assign a variable to itself, such as a = a.
# https://msdn.microsoft.com/en-us/library/a1kzfw0z(v=vs.90).aspx
# CS0067 - An event was declared but never used in the class in which it was declared.
# https://msdn.microsoft.com/en-us/library/858x0ycc(v=vs.90).aspx
# CS0649 - The compiler detected an uninitialized private or internal field declaration that is never assigned a value.
# https://msdn.microsoft.com/en-us/library/03b5270t(v=vs.90).aspx
# CS0618 - A class member was marked with the Obsolete attribute, such that a warning will be issued when the class member is referenced.
# https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/compiler-messages/cs0618
# CS0183 - The given expression is always of the provided ('type') type
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0183
# CS0184 - The given expression is never of the provided ('type') type
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0184
# CS1060 - Use of possibly unassigned field 'name'. Struct instance variables are initially unassigned if struct is unassigned.
# https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1060
# CS1058 - A previous catch clause already catches all exceptions
# https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1058
# CS0809 - Obsolete member 'memberA' overrides non-obsolete member 'memberB'.
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0809
# CS0672 - Member 'member1' overrides obsolete member 'member2. Add the Obsolete attribute to 'member1'
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0672
# CS0612 - 'member' is obsolete
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0612
# CS0626 - https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0626
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0626
# CS0684 - 'interface' interface marked with 'CoClassAttribute' not marked with 'ComImportAttribute'
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0684
# CS0824 - Constructor 'name' is marked external
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0824
# CS0688 - 'method1' has a link demand, but overrides or implements 'method2' which does not have a link demand. A security hole may exist.
# https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0688
# CS1522 - Empty switch block
# https://msdn.microsoft.com/en-us/library/x68b4s45(v=vs.90).aspx
# CS1570 - XML comment on 'construct' has badly formed XML
# https://msdn.microsoft.com/en-us/library/c20zzdxx(v=vs.90).aspx
# CS0465 - Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?
# https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0465
# CS1574 - XML comment on 'construct' has syntactically incorrect cref attribute 'name'
# https://msdn.microsoft.com/en-us/library/26x4hk2a(v=vs.90).aspx
# CS1998 - This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
# Link unavailable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment