StyleCop(VS拡張機能のC#スタイルチェック)
使い方
導入後、Visual Studioの「ツール」→「Run StyleCop」で実行。
「Run StyleCop」を実行すると、チェック結果がエラー一覧に表示される。(SA..で始まるエラーがそれに当てはまる)
StyleCopの設定
「ソリューション エクスプローラー」→「プロジェクト」を選択し、「StyleCop Sttings」を押下。
必要なものだけをチェックすることが可能。
チェック無効設定参考
以下、無効にした設定とその理由。
DocumentationRules
※英語を基準にしたルールのため、日本語でコメントを書く場合引っかかってしまうため。
種類 |
---|
SA1623: PropertySummaryDocumentationMustMatchAccessors(The property's documentation summary text must begin with:Gets or sets) |
SA1628: DocumentationTextMustBeginWithACapitalLetter |
SA1629: DocumentationTextMustEndWithAPeriod |
SA1630: DocumentationTextMustContainWhitespace |
SA1632: DocumentationTextMustMeetMinimumCharacterLength |
SA1642: ConstructorSummaryDocumentationMustBeginWithStandardText |
SA1643: DestructorSummaryDocumentationMustBeginWithStandardText |
SA1650: ElementDocumentationMustBeSpelledCorrectly |
FileHeader
※必ずFileHeaderを必須として付ける必要はないため。
種類 |
---|
SA1633 : CSharp.Documentation : The file has no header, the header Xml is invalid, or the header is not located at the top of the file. |
SA1634: FileHeaderMustShowCopyright |
SA1635: FileHeaderMustHaveCopyrightText |
SA1637: FileHeaderMustContainFileName |
SA1638: FileHeaderFileNameDocumentationMustMatchFileName |
SA1639: FileHeaderMustHaveSummary |
SA1640: FileHeaderMustHaveValidCompanyText |
SA1649: FileHeaderFileNameDocumentationMustMatchTypeName |
LayoutRules
種類 | 理由 |
---|---|
SA1515 : CSharp.Layout : A single-line comment must not be followed by a blank line. To ignore this error when commenting out a line of code, begin the comment with '////' rather than '//'. | コメントで//を使用するのが一般的であるため |
Maintainability Rules
種類 | 理由 |
---|---|
SA1401: Fields must be private | フィールドにprivate以外を使う可能性があるのと、フィールド名の命名規則は内部ルールに従うため |
SA1402:FileMayOnlyContainASingleClass | クラスが複数ある場合もあるため |
Naming Rules
種類 | 理由 |
---|---|
SA1307: Accessible fields must begin with upper case letter | フィールドにprivate以外を使う可能性があるのと、フィールド名の命名規則は内部ルールに従うため |
OrderingRules
種類 | 理由 |
---|---|
SA1307: Accessible fields must begin with upper case letter | usingを名前空間の外側に宣言するのも一般的であるため |
SA1210 : CSharp.Ordering : Using directives must be sorted alphabetically by the namespaces. | 低レイヤーからソートすることもあるため |
Readability Rules
種類 | 理由 |
---|---|
SA1121: Use the built-in type alias 'byte' rather than Byte or System.Byte. | 可読性を重視し、boolよりbooleanを使う場合もあると思うため |
SA1118: Parameter must not span multiple lines | パラメーター名が長かったり、パラメーターの数が多い場合、一行に宣言できない場合もあるので、チェックを必須にする必要はないため |
SA1122 : CSharp.Readability : Use string.Empty rather than "" | 空文字を入力するときに""を使用するほうが一般的なため |
SA1111 : The closing parenthesis or bracket must be placed on the same line as the last parameter in the parameter list. | 配列などの閉じ括弧は開き括弧と同じインデントにするため |
Spacing Rules
種類 | 理由 |
---|---|
SA1009 : Invalid spacing around the closing parenthesis. | 配列などの閉じ括弧は開き括弧と同じインデントにするため |
StyleCopのインポート
1、インポート前
2、「Settings.StyleCop」ファイルをインポートする
ファイルをプロジェクトフォルダの直下に入れることでインポート完了。
※「Settings.StyleCop」はプロジェクト単位で設定。
3、インポートされた内容の確認
「Settings.StyleCop」をプロジェクトディレクトリの直下に上書きすることで、同じチェック設定を共有出来る。
以上