Xamarin Studio Adds Roslyn Support

Xamarin Studio now supports Microsoft’s Roslyn compiler-as-a-service features found in Visual Studio 2015.  They’ve also announced a new project model that integrates with Microsoft’s MSBuild system. In previous versions of Microsoft’s Visual Studio, they had their C# compiler, but they separately had to write and maintain all of the code that supplied Intellisense, refactoring, red-squiggles,…

Free 1.5 Hour Video: Writing Roslyn Analyzers and Code Fixes

My deep abiding love of Roslyn continues! I just published a new video Writing Roslyn Analyzers and Code Fixes up at WintellectNOW: https://www.wintellectnow.com/Videos/Watch?videoId=writing-roslyn-analyzers-and-code-fixes. My goal was to take you from zero knowledge of Roslyn to writing a real world analyzer and code fix in 1.5 hours. This video covers everything from using the Syntax Visualizer…

Analyzing Control Flow with Roslyn

The other day my co-worker Jeffrey Richter and I were discussing my latest infatuation, Roslyn analyzers. As we bounced around a few ideas one came to the forefront; every catch block should throw. This is not a hard and fast rule, but eating an exception, especially accidentally, has caused more bugs in .NET than we…

More Wintellect.Analyzers and Some Lessons Learned Writing Roslyn Analyzers

Over the last few months I have been having a wonderful time developing Roslyn analyzers and code fixes. You can find all the Wintellect.Analyzers code at Wintellect’s GitHub page. If you would like to include these analyzers in your own Visual Studio 2015 CTP 6 project, install the NuGet package by executing the following in…

Interview with Matthew Gertz

Kathleen Dollard talks to Matthew Gertz, Principal Development Manager of Visual Studio Managed Languages at Microsoft about how they are becoming more transparent, ways in which the open source developer community helps Microsoft make better products, and the future of Roslyn.  

Interview with John Robbins

In this video we interview John Robbins about a new and open Microsoft, the potential of Roslyn, and how debugging has changed over the years. John Robbins is a cofounder of Wintellect, where he heads up the consulting and debugging services side of the business. He also travels the world teaching his Mastering .NET Debugging…

5 Questions with Kathleen Dollard

In this interview with Kathleen Dollard we dig into some of the new features of Visual Studio 2015, what’s coming up in the C# language, and the power of Roslyn and what it means for developers. Kathleen Dollard wants to teach you to code better. There’s more to learn about the tools you use every…

Wintellect.Analyzers: Five New Roslyn Diagnostic Analyzers and Code Fixes

When I first saw the Roslyn compiler, I was thrilled! For once the compiler was not going to be a black hole where source code comes in and on the other side of the worm hole a binary comes out. With open extensibility there’s going to be some amazing tools developed that were impossible to do any other way. With this week’s release of the Visual Studio 2015 Preview, the Roslyn API is solid and ready for extension!

We have posted on Wintellect’s GitHub account our first five analyzers and code fixers we wrote to explore that part of the API. They should give you a good idea how to get started writing your own new rules. Here’s the initial set of rules:

AvoidCallingMethodsWithParamArgs
This informational level gives you a hint that you are calling a method using param arrays. Because calls to these methods cause memory allocations you should know where these are happening.

AvoidPredefinedTypes
The predefined types, such as int, should not be used. When defining a type the built in types can be different sizes on different versions of .NET (e.g., desktop and Internet of Things) so you want to be as explicit about types as possible.

CallAssertMethodsWithMessageParameter
Calling the one parameter overload of Debug.Assert is a bad idea because they will not show you the expression you are asserting on. This analyzer will find those calls and the code fix will take the asserting expression and convert it into a string as the second parameter to the two parameter overload of Debug.Assert.

IfAndElseMustHaveBraces
If and else statements without braces are reasons for being fired. This analyzer and code fix will help you keep your job. :) The idea for this analyzer was shown by Kevin Pilch-Bisson in his awesome TechEd talk on Roslyn. I just finished it off.

ReturningTaskRequiresAsync
If you are returning a Task or Task<T> from a method, that method name must end in Async.

There’s no real secret to writing rules. The Roslyn API is extremely well done and once you start getting a feel for it, you can quickly see how things fit together. The best thing you can do is just spend time looking at the Roslyn Syntax Visualizer to see how syntax and tokens fit together. I personally found right clicking on the expression and looking at the Directed Syntax graph was super helpful.

Feel free to add your rules to our repository. I’ve got a couple of more I’m working and so do other folks at Wintellect so subscribe so star the repo to get notifications of updates.