Skip to content

WolframResearch/codeinspector

Repository files navigation

CodeInspector

CodeInspector is a package for finding and reporting problems in Wolfram Language code. CodeInspector has a large collection of rules that can be used to inspect Wolfram Language source code files and can be customized to your preferences.

In a stand-alone kernel:

Needs["CodeInspector`"]

CodeInspectSummarize["If[a,b,b]"]
Out[2]= If[a,b,b]

        line 1:  If[a,b,b]
                      ^ ^  
                DuplicateClauses Error Both branches are the same.

In a front end:
CodeInspectSummarize

Static Analysis Tools in the Wolfram Language

"CodeParser and CodeInspector" on community.wolfram.com

Finding Bugs in the Wolfram Language from WTC 2019: Watch Video (youtube)

Finding Bugs in the Wolfram Language from WTC 2019: Watch Video (wolfram.com)

Finding Bugs in the Wolfram Language from WTC 2019: Download Presentation

Setup

CodeInspector depends on CodeParser paclet and CodeFormatter paclet.

CodeInspector and its dependencies are included in Mathematica 12.2 and above.

For older versions, install CodeInspector paclet and dependencies from the public paclet server:

PacletInstall["CodeParser"]
PacletInstall["CodeFormatter"]
PacletInstall["CodeInspector"]

Build and install the CodeInspector paclet locally

Using CodeInspector

After CodeParser and CodeInspector are installed, CodeInspector can be used.

Return the list of all problems found in a string of code:

Needs["CodeInspector`"]

CodeInspect["If[a,b,b]"]
Out[2]= {DuplicateClauses Error Both branches are the same.}

Summarize the problems found in a source code file:

Collatz

The input to CodeInspect and CodeInspectSummarize may be a string, a File, or a list of bytes.

See this tutorial in the Wolfram System help browser:

  • CodeInspector/tutorial/CodeInspectorTutorial

Troubleshooting

Make sure that the paclets can be found on your system:

Needs["CodeInspector`"]

and try a basic example:

CodeInspect["If[a, b, b]"]