最近需要做一个C#项目的代码走查,其中的一项内容就是要用代码的静态分析工具对代码进行检测,我的想法是使用VisualStudio自带的Microsoft托管建议规则外加StyleCop工具检测问题(主要是警告),再对这些问题逐一分析。
我使用的VisualStudio版本为2012,Windows版本为Win7旗舰版。一般来说,编译C#代码时VS都会按照Microsoft托管建议规则给出编译报错和警告信息,并支持用户自己配置个性化的ruleset。对于每一个配置项,都可以在MSDN上找到详细的说明文档,地址如下:
https://msdn.microsoft.com/zh-cn/library/ee1hzekz.aspx
(这些规则专注于代码中最关键的问题,包括潜在的安全漏洞、应用程序崩溃和其他重要的逻辑和设计错误。您应在为项目创建的任何自定义规则集中包含此规则集,规则的ruleset文件地址在VS安装目录下的\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset)
StyleCop支持了另外一个思路的规则,即代码的书写风格。
StyleCop下载地址:http://stylecop.codeplex.com/
我下载的StyleCop版本为4.7.54.0,安装包名为StyleCop-4.7.54.0.msi
第一步,欢迎界面
第二步,授权协议(Microsoft Public License,详见附文)
第三步,自定义安装,其中Visual Studio integration支持VS2008、2010、2012、2013
第四步,确定安装
第五步,读条界面(时间还是比较长的)
第六步,安装完毕
安装完毕后,打开VisualStudio,在程序集上单击鼠标右键,就可以看到多出的三个选项了。
Run StyleCop、Run StyleCop(Rescan All)可用于运行StyleCop做代码检查
StyleCop Settings可设定StyleCop的代码检查规则,设定界面如下:
检查规则设置完毕后,可在程序集所在目录下看到Settings.StyleCop文件,该文件保存了这些检查规则,并可用于下次检查,或复制到其他程序集下,让其他程序集的代码检查也应用这一规则。每次检查后的结果存储在文件StyleCop.Cache里。
StyleCop对C#语言的检查项大致分为以下几类:
-Documentation Rules 文档化注释规则
-Element Documentaion 变量的文档化注释
-File Headers 文件头部
-Layout Rules 布局规则
-Curly Brackets 花括号位置
-Line Spacing 行距
-Maintainability Rules 稳定性规则
-Access Modifier 访问修饰符
-Dubug Test 调试信息
-File Contents 文件内容
-Parenthesis 圆括号
-Removable Code 可移除的代码
-Naming Rules 标识符命名规则
-Ordering Rules 排序规则
-Element Order 变量顺序
-Using Directives 命名空间引用顺序(Using指令)
-Readability Rules 可读性规则
-Comments 注释
-Member Access 成员访问
-Method Parameter Placement 变量位置
-Query Expressions 查询表达式
-Regions Regions区域
-Statements 声明
-Strings 字符串
-Types 类型
-Spacing Rules 空格规则
附:Microsoft Public License全文
Microsoft Public License (Ms-PL)
This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
1. Definitions
The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the same meaning here as under U.S. copyright law.
A “contribution” is the original software or any additions or changes to the software.
A “contributor” is any person that distributes its contribution under this license.
“Licensed patents” are a contributor’s patent claims that read directly on its contribution.
2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors’ name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed “as-is.” You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.