From ab1984c8ac07fea86d1779956fd4f9ebfbb91b9e Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 21 Jun 2022 12:49:02 -0700 Subject: feat(wpcarro/blog): Create short post about C# Inspired by Profpatsch's "Notes", here's a short post about a thing that happened today... Note: I'd like a way to convert `git log --format=%b` into blog posts (maybe). Maybe some commit metadata like `POST=true` Change-Id: I492c48c81891d9f64da8c8149d0d2bfa864ed731 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5889 Tested-by: BuildkiteCI Reviewed-by: wpcarro Autosubmit: wpcarro --- users/wpcarro/website/blog/posts.nix | 7 ++++ .../website/blog/posts/csharp-unused-variables.md | 40 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 users/wpcarro/website/blog/posts/csharp-unused-variables.md diff --git a/users/wpcarro/website/blog/posts.nix b/users/wpcarro/website/blog/posts.nix index 5a58c9309c..b9345a5ca1 100644 --- a/users/wpcarro/website/blog/posts.nix +++ b/users/wpcarro/website/blog/posts.nix @@ -29,4 +29,11 @@ content = ./posts/auto-reboot-nixos.md; draft = false; } + { + key = "csharp-unused-variables"; + title = "Unused Variables Broke Prod"; + date = 1655840877; + content = ./posts/csharp-unused-variables.md; + draft = false; + } ] diff --git a/users/wpcarro/website/blog/posts/csharp-unused-variables.md b/users/wpcarro/website/blog/posts/csharp-unused-variables.md new file mode 100644 index 0000000000..0d26a86cca --- /dev/null +++ b/users/wpcarro/website/blog/posts/csharp-unused-variables.md @@ -0,0 +1,40 @@ +# Unused Variables Broke Prod + +**Problem**: This morning we broke production because (believe it or not) an +unused variable went undetected. + +**Solution**: Consume the variable in the relevant codepath. + +**Further AI**: Treat unused variables as errors (which will block CI). + +## Warning/Disclaimer + +I am not a C# programmer. I know close to nothing about C#. But at `$WORK`, one +of our codebases is written in C#, so occasionally I interface with it. + +## Treating Unused Variables as Errors + +C# uses `.csproj` files to configure projects. The following changes to our +`.csproj` file WAI'd: + +```diff ++ ++ IDE0059 ++ true +``` + +However, supporting this turned out to be a ~1h adventure... Why was this +unexpectedly difficult? As it turns out, there are the 3x promising compiler +warnings that I had to discover/try: + +- `CS0219`: doesn't WAI (see "Note" here: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0219) +- `CA1804`: silently unsupported (replaced by `IDE0059`) +- `IDE0059`: WAIs + +Legend: +- `CS`: stands for C# +- `CA`: stands for Code Analysis (I *think* a Visual Studio concept) +- `IDE`: stands for IDE (I think *also* a Visual Studio concept) + +For `CA` and `IDE` prefixed warnings, `EnforceCodeStyleInBuild` must also be +enabled. -- cgit 1.4.1