Lints and Hints: What tools are available for checking and correcting the conventions, style and common errors in my language (lint tools), and are there tools that can automatically fix them (hint tools)?
This is a follow-up to a comment posted on Is there a place for automated code reviews?
Would a list of static analysis tools help jumpstart this? – h.j.k. 34 mins ago
Let's list a few tools that people can use to address easy and common issues in their code before they ask their questions, and that can help reviewers gather "easy" points to mention in their CR answers...:
- Bash (and other shells)
- C, C++ also C specific
- C#
- Delphi
- HTML, XHTML, CSS
- Go
- Java
- JavaScript
- jQuery
- JSON
- Lisp
- Lua
- MongoDB
- Objective-C
- Perl
- PHP
- Python
- Regex
- Ruby
- Scala
- SQL
- Swift
- VB.NET
- VB6 and VBA
- XML
To keep this organized, please post one tool/language per answer; if it's a paid/commercial tool, please mention it. Also please include a link to the website.
-
2\$\begingroup\$ This should also help: en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis \$\endgroup\$JaDogg– JaDogg2015年05月02日 04:34:21 +00:00Commented May 2, 2015 at 4:34
-
2\$\begingroup\$ Related: meta.codereview.stackexchange.com/questions/5283/… \$\endgroup\$JaDogg– JaDogg2015年05月03日 16:36:06 +00:00Commented May 3, 2015 at 16:36
-
1\$\begingroup\$ Shouldn't the title start with "Tools for formatting"? (Not "Tools for format".) \$\endgroup\$Peter Mortensen– Peter Mortensen2022年03月17日 19:52:30 +00:00Commented Mar 17, 2022 at 19:52
26 Answers 26
Python
Python uses Python Enhancement Proposals (PEPs) to improve Python, ranging from style guides to feature requests. There are linters for a few of these:
PEP 8 -- Style Guide for Python Code.
There are both online (pep8online.com) and offline (pycodestyle h ) versions for this.
PEP 257 (pydocstyle h ) -- Docstring Conventions.
There is also "PEP 7 -- Style Guide for C Code [in the C implementation of Python]". But this does not seem to have a linter.
When starting a new project you might choose to do this:
- "$ ruff check" -- fast and "not too picky" linter, you should probably do what it suggests, or let
--fix
do that for you. - "$ isort ." -- "I sort imports so you don't have to", per PEP8, so just do it.
- "$ black ." -- will reformat source code, in an opinionated way.
The first two are no-brainers, they won't hurt anything.
Adopting black
can be a bit more of a hurdle,
but once you bite the bullet there's all manner of
trivial pull-request code review remarks that simply
never arise any more.
Blacked code will typically lint clean.
Start with "$ black -S ." followed by "git diff".
The -S
says don't turn single- into double- quote strings,
basically noise when you're viewing the initial diffs.
Go through some commits, plus one or two PRs,
and watch the S/N ratio improve.
There are also some other analysis tools:
-
Warning: This imports the code to analyse it.
-
Combines Pyflakes and pycodestyle into one tool.
-
Combines all of: pylint, Pyflakes, pycodestyle, pydocstyle and more tools.
There are also ways to incorporating some of the above tools into different editors. For a small list of incorporations:
- Sublime Text has SublimeLinter 3,
- vim has pylint.vim and pep8,
- Atom has linter-pep8,
- PyCharm has PEP 8 by default,
- Spyder has PEP 8 support by default, and
- Visual Studio Code has the Python extension that can work with a long list of linters, e.g. Pylint and Flake8.
These are good to make sure your answers are PEP 8 compliant, and so there are no contradictions when saying to follow PEP 8. However using the tools from the above sections will give a report that you can comment from.
C / C++
Clang's Static Analyzer, a free static analyzer based on Clang.
Coverity, a commercial static analyzer, free for open source developers.
Cppcheck, an open-source static analyzer.
CppDepend, a commercial static analyzer based on Clang.
GCC, free compiler that closely follows C and C++ standards.
Valgrind, run-time analyser that is particularly good for the kind of memory errors common in these languages.
Simply compiling code with warning options such as -Wall -Wextra -pedantic
catches quite a number of potential or actual problems.
C#
StyleCop (free)
It can be run from inside of Visual Studio or integrated into an MSBuild project.
ReSharper (commercial)
A paid Visual Studio extension (possibility for open-source "community" license though) with a free 30-day trial.
FxCop (free)
Can be run standalone, but later versions are bundled into Visual Studio as the "Code Analysis" feature.
VB6 / VBA6 / VBA7 (x86/x64)
- Rubberduck is a free (beer & speech), open-source add in for the VBE IDE maintained by several members of the vba and c# community here on Code Review. For more info see the rubberduck tag.
Features include static code analysis, code metrics, refactoring tools, Smart Indenter, a unit testing framework, and more.
Direct Link to Code Inspection Documentation
JavaScript
For JavaScript, there are a few options:
- JSLint (the original, created by Douglas Crockford)
- JSHint (a community-driven fork of JSLint)
- ESLint (an ES6-compatible fork of JSHint. Necessary if you want to lint ES6 code).
In all cases, if you're using a transpiler like Babel or Traceur, you should run the linter before the transpile step. Don't lint compiled code.
Note that all major browsers and other interpreters support the "use strict";
directive in code, and will ensure that many common mistakes in code are identified before running the code. See: What does "use strict" do in JavaScript, and what is the reasoning behind it?
VB.NET
ReSharper (commercial)
A paid Visual Studio extension (possibility for open-source "community" license though) with a free 30-day trial.
-
\$\begingroup\$ As of the Visual Studio 2015 RC, some of this functionality is built in. visualstudio.com/news/vs2015-vs#ManLang \$\endgroup\$RubberDuck– RubberDuck2015年05月01日 15:55:00 +00:00Commented May 1, 2015 at 15:55
-
1\$\begingroup\$ @RubberDuck Yeah, VS ended up implementing an actual "rename" refactoring, too. But VS just isn't the same without R# ;) \$\endgroup\$Mathieu Guindon– Mathieu Guindon2015年05月01日 15:56:58 +00:00Commented May 1, 2015 at 15:56
HTML5, XHTML, CSS
Tool: Markup validation checker
Also, there are beautifiers to help improve formatting, CodeBeautify.org has a good one.
Java
Java lint tools are usually dependent of the IDE you develop in. All the major IDE's (Eclipse, IntelliJ, Netbeans, etc. - alphabetical order) have mechanisms in place for not only checking for lint-like problems, but also for fixing them too.
Features to expect from your IDE - identification of, and correction of:
- automatic code formatting (indentation, brace positions, line-wrapping, etc.)
- redundant code, or impossible code
- incomplete documentation
- variable and function name "shadowing"
- and much, much more.
Standalone tools:
-
2\$\begingroup\$ There is also a bunch of maven plugins that can be used \$\endgroup\$Simon Forsberg– Simon Forsberg2015年05月02日 11:14:39 +00:00Commented May 2, 2015 at 11:14
-
\$\begingroup\$ Funny enough running PMD on PMD reveals the exact thing it's trying to correct ;) \$\endgroup\$Bono– Bono2015年05月06日 14:35:48 +00:00Commented May 6, 2015 at 14:35
Regex:
JS-Flavored
Debuggex.com - online regex visual debugger
Regexr.com - online regex tester
Regex101.com - online tester and debugger
Offline-Tools:
- RegexBuddy - commercial regex-development helper tool, supporting a multitude of regex-flavors and programming languages
C
Although not currently actively developed, this free, open-source software provides error checking that can be enhanced with in-code commenting.
SQL
General:
Program: Mimer SQL Validator
Online: FreeFormatter.com
SQL Server / Transact-SQL
Online: SQL-Format.org
Natively: SET SHOWPLAN_XML
MySQL
Online: MySQL Syntax Check
Natively: EXPLAIN
PostgreSQL & PL/pgSQL
Plugin: pg-validator
Natively: EXPLAIN
SQLite
Plugin: sqllogictest
Natively: EXPLAIN
Oracle:
Natively: EXPLAIN
-
4\$\begingroup\$ Can I be on this list? \$\endgroup\$nhgrif– nhgrif2015年05月01日 18:49:15 +00:00Commented May 1, 2015 at 18:49
-
1\$\begingroup\$ The real question is: Do you really want to? ;-) \$\endgroup\$Phrancis– Phrancis2015年05月01日 19:01:52 +00:00Commented May 1, 2015 at 19:01
-
\$\begingroup\$ Note
Atom.io
is a text-editor.linter
is a plug-in for Atom providing the necessary checks for many, many languages. \$\endgroup\$2015年05月01日 21:44:56 +00:00Commented May 1, 2015 at 21:44 -
\$\begingroup\$ Good catch, I corrected it! \$\endgroup\$Phrancis– Phrancis2015年05月01日 21:48:21 +00:00Commented May 1, 2015 at 21:48
MongoDB
Plugins:
Mongoose (for Node.js)
revalidator (for Node.js)
lx-valid (for Node.js)
Mongoid (for Ruby)
Or natively use validate
Ruby
Gems:
- rubocop is probably the most popular
But there are many more each with emphasis on different things (style, code complexity, etc.).
Online:
- Don't know if this counts as it's more like CI, but there's always CodeClimate (also works for JavaScript, PHP, and Python)
Swift
Go
golint
Style and convention problems with documentation, naming, dead-code analysis, and more.go fmt
adjusts your go code to conform to standard Go style (indentation, spacing, import orders, etc.)go vet
does deeper static analysis on your code and determines if there are more serious problems in the code, including poorly structured printf statements, etc.
In addition, there are profiling tools, race-condition-analysis tools, and so on that work at program runtime.
JSON
Online validator: JSONLint.com
They also have a free Pro Version
For those at the linux command line, use jq
for all sorts of cool features.