M-Lint Code Analyzer

What Is the M-Lint code Analyzer?

The M-Lint code analyzer checks your code for problems and recommends modifications to maximize performance and maintainability.

Ways to Use M-Lint

You can use M-Lint in two different ways, both of which report the same information:

M-Lint Automatic Code Analyzer in the Editor

To use the M-Lint continuous code checking in an M-file in the Editor, perform the following steps:

  1. Ensure the M-Lint messaging preference is enabled: Select File > Preferences > M-Lint and select the Enable integrated M-Lint warning and error messages check box. To follow these instructions, be sure the Underlining option is set to Underline warnings and errors.

    Image of M-Lint Preferences.

  2. Click OK.

  3. Open an M-file in the Editor. This example uses the sample file lengthofline.m:

    1. Open the example file:

      open(fullfile(matlabroot,'help','techdoc','matlab_env',...
           'examples','lengthofline.m'))
      
    2. Save the example file to a directory to which you have write access. For the example, lengthofline.m is saved to I:\MATLABFiles\mymfiles.

  4. The M-Lint message indicator at the top right edge of the window conveys the M-Lint messages reported for the file:

    For the example, the indicator is red, meaning there is at least one error in the file.

    Image of M-Lint lengthofline.m file open in Editor with M-Lint message indicator bar called out.

  5. Click the M-Lint message indicator to go to the next code fragment containing an M-Lint message. The next code fragment is relative to the current cursor position, viewable in the status bar.

    In the lengthofline example, the first message is at line 22. The cursor moves to the beginning of line 22.

  6. The code fragment for which there is an M-Lint message is underlined in either red for errors or orange for warnings and improvement opportunities.

    To view the M-Lint message, move the pointer within the underlined fragment. The message appears with a yellow highlighted background, similar to data tips (see Viewing Values as Data tips in the Editor).

    Image of M-file and M-Lint message.

    This message means that in line 22, nothandle is assigned a value, but is probably not used anywhere after that in the file. The line might be extraneous and you could delete it. But it might be that you actually intended to use the variable, as shown in step 6 of this example.

  7. Make changes to your code as needed. The M-Lint indicator and underlining automatically update to reflect the changes you make, even if you do not save the file.

    In this example, the intention was to use nothandle as a performance improvement by determining the value prior to the loop. Changing ~ishandle(hline(nh)) in line 24 to nothandle(nh) means there is no longer an M-Lint message associated with line 22. For more information about what the warning and improvement messages in this example mean and actions you can take to address them, see Messages and Resulting Changes for the lengthofline Example.

  8. Some errors and warnings are highlighted, indicating M-Lint can automatically fix the code. For example, in lengthofline, line 23, prod is underlined because there is an M-Lint warning, and it is highlighted because an automatic fix is available. When you view the M-Lint message, it also indicates the auto-fix that is available.

    Image of M-Lint lengthofline.m file with message indicating auto-fix available.

    Image of M-Lint lengthofline.m file with auto-fix menu item.

    Right-click the highlighted code (for a single-button mouse, use Ctrl+click). The first item in the context menu indicates the automatic fix that M-Lint can perform. Select it and M-Lint automatically corrects the code. In this example, M-Lint replaces prod(size(hline)) with numel(hline).

    Image of M-Lint lengthofline.m file after auto-fix for line 23 was done.

    There is a preference you can set for the color—for more information, see Other Colors.

  9. You might want to ignore certain M-Lint messages and do not want the messages to display; for more information, see Suppressing M-Lint Indicators and Messages.

  10. You can click the M-Lint message indicator to go to the next message, or use the other way to view messages, which is the M-Lint message bar. Each marker in the bar represents a line that has associated M-Lint messages. A red marker means there is an error at that line, while an orange marker means there are warnings or suggested improvements, but no errors at that line.

    1. Position the pointer at a marker in the message bar to view the message. For example, to see an error in lengthofline, position the pointer at a red marker in the message bar. There is only one error in the file and with the pointer positioned over it, the associated M-Lint messages appears. Click the marker to go to the first code fragment in the line that resulted in an M-Lint message. For the example, click the red marker, which takes you to the first suspect code fragment in line 48.

      temp = diff([data{1}(:) data{2}(:) data{3}(;)]);

      Multiple messages can represent a single problem or multiple problems. Addressing one might address all of them, or after addressing one, the other messages might change or what you need to do might become clearer.

      Image of M-file showing M-Lint message bar and message associated with red marker.

    2. Make changes to address the problem noted in the M-Lint message—the M-Lint indicators update automatically.

      In the example, the M-Lint message suggest a delimiter imbalance. You can check that by moving the arrow key over each of the delimiters to see if MATLAB® software indicates a mismatch. This requires that File > Preferences > Keyboard > Delimiter Matching has the Match on arrow key option selected. There are no mismatched delimiters. The actual problem is the semicolon in parentheses, data{3}(;), is incorrect and should be a colon. In line 48, change data{3}(;) to data{3}(:). When you make the change, the underline no longer appears in line 48. That single change addressed the issues in all of the M-Lint messages for line 48.

      Because the change you made removed the only error in the file, the M-Lint message indicator at the top of the bar changes from red to orange, indicating that only warnings and potential improvements remain.

    3. If there are multiple messages associated with a line, there might be multiple underlined code fragments that are adjacent, as in the above example, making it difficult to display the message of interest. In those cases, it might be easier to view the messages via the marker on the message bar.

  11. After making changes to address all M-Lint messages, or disabling designated messages, the M-Lint message indicator becomes green The example file with all M-Lint messages addressed has been saved as lengthofline2.m, which you can open by running

    open(fullfile(matlabroot,'help','techdoc',...
    			'matlab_env', 'examples','lengthofline2.m'))
    

    Image of M-file with green M-Lint message indicator.

Suppressing M-Lint Indicators and Messages

Depending on what stage you are at in completing the M-file, you might want to restrict the underlining, which you can do via the M-Lint preference referred to in step 1, above. For example, when first coding, you might prefer no underlines because they would be distracting. For details, click the Help button in the Preferences dialog box.

M-Lint does not provide perfect information about every situation and in some cases, you might not want to make any changes based on an M-Lint message. In the event you do not want to change the code but you also do not want to see the M-Lint indicator and message for that line, instruct M-Lint to suppress them. For the lengthofline example, in line 49, the first M-Lint message is Terminate statement with semicolon to suppress output (in functions). Adding a semicolon to the end of a statement suppresses output and is a common practice. M-Lint alerts you to lines that produce output but lack the terminating semicolon. If you want to view output from line 49, do not add the semicolon as M-Lint suggests.

There are a few different ways to suppress the M-Lint indicators and messages:

Note that you cannot suppress M-Lint error messages such as syntax errors, and therefore, the following options do not apply.

Ignore Only a Specific Instance

Right-click at the M-Lint underline (for a single-button mouse, use Ctrl+click). From the context menu, select Ignore this "Terminate statement with semicolon...". M-Lint adds a %#ok<NOPRT> to the end of the line, which instructs MATLAB not to check for a terminating semicolon at that line. M-Lint removes the underline and mark in the M-Lint indicator bar for that message.

If there are two messages on a line that you do not want M-Lint to display, right-click separately at each underline and select the appropriate entry from the context menu. M-Lint expands the %#ok syntax. For the example, ignoring both messages for line 49 would add %#ok<NBRAK,NOPRT>.

For more information about %#ok, see the mlint function reference page.

This method of suppressing the messages changes the M-file. If M-Lint preferences are to set to enable this message, the specific instance of the message suppressed in this way will not appear because the %#ok takes precedence over the preference setting. If you later decide you want M-Lint to check for a terminating semicolon at that line, delete the %#ok<NOPRT> from the line.

Image of Editor with file open in it. Right-click at an M-Lint underline and select the option instructing M-Lint to ignore only this instance of the message.

Image of Editor with file open in it and %#ok added at the end of a line to indicate that you want the M-Lint message suppressed for this particular line of code.

Disable All Instances in All Files

Right-click at the M-Lint underline (for a single-button mouse, use Ctrl+click). From the context menu, select Disable all "Terminate statement with semicolon...". Doing so modifies the M-Lint preference setting, which applies to all occurrences in all files, unless a line includes a %#ok for that message. For more information about the M-Lint preference, including how to restore MATLAB default settings, select File > Preferences > M-Lint, and click Help.

Disable Specified Messages or in Selected Files as Needed

Use M-Lint preferences by selecting File > Preferences > M-Lint. Then enable specific messages or categories of messages and save the settings to a txt file. You can reuse the settings for any M-file, or provide the settings file to another user.

To use the saved settings, either select the settings file in M-Lint preferences, or in the Editor. In the Editor, right-click the M-Lint message bar (for a single-button mouse, use Ctrl+click), or select Tools > M-Lint. The currently-selected setting choice is shown, preceded by a bullet point. You can choose from any of the settings files, such as the MLintNoSemis example, as shown here.

Image of Editor with file open in it and the Tools > M-Lint menu open. The M-Lint default settings are currently selected as indicated by a bullet point preceding that menu item. You can select any text file to use the M-Lint settings specified in that file.

For more information about M-Lint settings and preferences, click Help in the M-Lint preferences panel.

About M-Lint and Unexpected MATLAB® Termination

Under some circumstances, when you are editing an M-file, M-Lint can cause the MATLAB session to terminate unexpectedly. The next time you start MATLAB, it displays the following message and disables M-Lint for the M-file that was open in the editor when MATLAB terminated.

M-Lint caused your previous MATLAB session to terminate unexpectedly. 
Please send this message and file name 
to The MathWorks. See "About M-Lint and Unexpected MATLAB Termination" 
in the MATLAB documentation for details.

If you want, while waiting for a response from The MathWorks™, you can attempt to reenable M-Lint for the file by following these steps:

  1. In the Editor, reopen the file that you were editing when MATLAB terminated.

  2. Remove the lines of code that you believe M-Lint could not handle.

  3. In a text editor, open the MLintFailureFiles file in your preferences directory. (This is the directory that MATLAB returns when you run prefdir.)

  4. Save and reopen the M-file.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS