Configure Code Analyzer
The Code Analyzer checks code and provides information about errors and warnings. To
configure the Code Analyzer, place a file named
codeAnalyzerConfiguration.json
in a resources
folder. In this configuration file, you can add custom checks and modify existing checks
of the Code Analyzer. Configure the Code Analyzer to enable or disable messages,
customize message text, or change the severity of the displayed message.
Note
Changing the severity of a Code Analyzer message does not affect execution. Code
is still executed even if the severity of a check has been set to
error
. Syntax errors still prevent execution even if their
severity is changed or the message disabled.
This file configures the Code Analyzer checks performed for the
resources
folder's parent folder and its subfolders. The
configuration is cached at the start of a MATLAB® session. The MATLAB Editor does not automatically get the latest configuration when you add a
new configuration file to a resources
folder or update an existing
file during a MATLAB session. To refresh the cache, call matlab.codeanalysis.refreshConfiguration
.
Verify that the file is a valid JSON file using the matlab.codeanalysis.validateConfiguration
function.
Sample Configuration File
This code shows the contents of a sample configuration file. The
codeAnalyzerConfiguration.json
file uses JSON format, and
//
designates the text that follows as a comment.
{ // Configuration File Information "name": "Acme Corp Guideline", "description": "Internal MATLAB Coding Guideline", "author" : "Alex", "schemaVersion" : "1.0.0", "guidelineVersion" : "1.0.0", // Base Configuration Settings "baseConfiguration" : "closestParentFolder", // New and Modified Checks "checks": { "MyFunctionCheck" : { "rule": { "template": "functionCall", "functionNames" : "evalin" }, "severity" : "error", "messageText" : "Do not use evalin.", "enabled" : true }, "MyVariableCheck" : { "rule": { "template": "variableName", "variableNames" : ["size", "error", "length", "max", "isa", "nargin" , "numel" , "nargout" ,"isequal" , "zeros" , "true" ,"false" ,"fullfile" ,"find", "get"] }, "severity" : "warning", "messageText" : "Avoid using function names for variables.", "enabled" : true }, "LLMNC" : { "severity" : "error", "limit" : 10, "enabled": true }, "FCNOL" : { "severity" : "error", "messageText": "Too many outputs.", "limit" : 15, "enabled": true }, "AGROW" : { "severity" : "info", "messageText" : "Growing array in a loop is not recommended", "enabled" : false } } }
Configuration File Information
You can optionally include the following properties in the configuration file. These properties do not affect the configuration. Each property accepts a string containing the relevant information.
"name"
— Name of the configuration file"description"
— Description of the configuration file"author"
— Author name"schemaVersion"
— Schema version in the format"1.2.3"
"guidelineVersion"
— Guideline version in the format"1.2.3"
Base Configuration Settings
A configuration file can inherit the rules of a configuration file contained in
the resources
folder of a parent folder. The property
"baseConfiguration"
specifies what base configuration should
be used and accepts these values:
"closestParentFolder"
(default) — Use the configuration file found in the closest parent folder. If"baseConfiguration"
is not defined, then the"closestParentFolder"
setting is used."factory"
— Use the standard MATLAB Code Analyzer configuration.
Add Custom Checks for Existing Functions
You can configure the Code Analyzer to display a check when specific functions are
used. To create a new check, you must assign a check ID that is a valid MATLAB
identifier. For example, define "MyFunctionCheck"
to check for an
evalin
function call.
The "MyFunctionCheck"
check has these properties:
Property Name | Example | Description |
---|---|---|
"rule" | "rule": { "template" : "functionCall", "functionNames" :
"evalin" } | Define the rules for the custom check. The
|
"messageText" (optional) | "messageText" : "Do not use evalin." | Specify the text displayed in the Code Analyzer message. |
"severity" (optional) | "severity" : "error" | Specify the check severity as "warning" ,
"error" , or "info" .
Messages that have been changed to "error" do not
prevent execution. |
"enabled" (optional) | "enabled": true | Specify whether this check is enabled in the MATLAB Editor. |
Add Custom Checks for Specified Variable Names
You can configure the Code Analyzer to display a check when specific variable
names are used. To create a new check, you must assign a check ID that is a valid
MATLAB identifier. For example, define "MyVariableCheck"
to check
for variables with the names size
, error
, or
length
.
The "MyVariableCheck"
check has these properties:
Property Name | Example | Description |
---|---|---|
"rule" | "rule": { "template" : "variableName", "variableNames" :
["size", "error", "length"] } | Define the rules for the custom check. The
|
"messageText" (optional) | "messageText" : "Avoid using function names for
variables." | Specify the text displayed in the Code Analyzer message. |
"severity" (optional) | "severity" : "warning" | Specify the check severity as "warning" ,
"error" , or "info" .
Messages that have been changed to "error" do not
prevent execution. |
"enabled" (optional) | "enabled": true | Specify whether this check is enabled in the MATLAB Editor. |
Configure Maximum Input and Output Arguments
You can configure the Code Analyzer to limit the number of input and output
arguments for a function. Use the check ID "FCNIL"
to specify the
maximum number of input arguments for a function. Use the check ID
"FCNOL"
to specify the maximum number of output
arguments.
The "FCNIL"
and "FCNOL"
checks have these
properties:
Property Name | Example | Description |
---|---|---|
"messageText" (optional) | "messageText" : "Too many outputs." | Specify the text displayed in the Code Analyzer message. |
"severity" (optional) | "severity" : "error" | Specify the check severity as "warning" ,
"error" , or "info" .
Messages that have been changed to "error" do not
prevent execution. |
"limit" (optional) | "limit" : 10 | Specify the maximum number of arguments. |
"enabled" (optional) | "enabled": true | Specify whether this check is enabled in the MATLAB Editor. |
Configure Maximum Characters per Line, Lines in a Function, and Nested Control Statements
You can configure the maximum characters per line, lines in a function, and nested
control statements. Use the check ID "FCNLL"
to specify the
maximum number of lines of code for a function. Use the check ID
"LLMNC"
to specify the maximum number of characters
(including whitespaces) in a line. Use the check ID "MNCSN"
to
specify the maximum number of control statements, such as for
and
if
, that can be nested.
The "FCNLL"
, "LLMNC"
, and
"MNCSN"
checks have these properties:
Property Name | Example | Description |
---|---|---|
"messageText" (optional) | "messageText" : "Too many outputs." | Specify the text displayed in the Code Analyzer message. |
"severity" (optional) | "severity" : "error" | Specify the check severity as "warning" ,
"error" , or "info" .
Messages that have been changed to "error" do not
prevent execution. |
"limit" (optional) | "limit" : 10 | Specify the maximum number of arguments. |
"enabled" (optional) | "enabled": true | Specify whether this check is enabled in the MATLAB Editor. |
Modify Existing Code Analyzer Checks
You can use the configuration file to modify existing Code Analyzer checks. Use
the check ID you want to modify as the check name. Then, specify the properties you
want to modify and their new values. For example, to disable a check in the editor,
specify "enabled" : false
.
To identify the check ID for checks in a given file, use codeIssues
. For a full
list of configurable checks, see Index of Code Analyzer Checks.
The built-in Code Analyzer checks have these properties:
Property Name | Example | Description |
---|---|---|
"messageText" (optional) | "messageText" : "Growing array in a loop is not
recommended" | Specify the text displayed in the Code Analyzer message |
"severity" (optional) | "severity" : "info" | Specify the check severity as "warning" ,
"error" , or "info" .
Messages that have been changed to "error" do not
prevent execution. |
"enabled" (optional) | "enabled" : true | Specify whether this check is enabled in the MATLAB Editor. |