SlideShare a Scribd company logo
1 of 12
Linter – static analysis for Scala
Linter
static analysis for Scala
(Scala Workshop, 02.07.2013)
Matic Potočnik - @HairyFotr
University of Ljubljana
Linter – static analysis for Scala
What is Linter?
Scala compiler plug-in
for doing static analysis
started by Jorge Ortiz
Linter then... 6 checks
Linter now... ~70 checks
Linter – static analysis for Scala
How does it work?
Pattern matching
on the Scala AST
Abstract interpretation,
mostly for Int and String
Linter – static analysis for Scala
Option and collection related checks:
• Replace if(opt.isDefined) opt.get else alternative
with opt.getOrElse(alternative)
• Replace col.find(x => cond).isDefined
with col.exists(x => cond)
• Replace col.flatMap(x => if(cond) Some(x) else None)
and col.flatMap(x => if(cond) List(x) else Nil)
with col.filter(x => cond)
• Using opt.size is practically always by mistake
• ...
Linter – static analysis for Scala
Numeric math checks:
• Loss of precision
• Use log1p(x) instead of log(1 + x)
• Use expm1(x) instead of exp(x) – 1
• BigDecimal(0.555555555555555555555)
... we get 0.5555555555555556
• Manually calculating functions like
abs, signum, isNaN, ...
Linter – static analysis for Scala
Repeated part of condition:
if(data.isEmpty && arg == “shortname” || arg == “name”) {
...
} else if(arg == “shortname” || arg == “name”) {
...
} else ...
Warning: This condition has appeared earlier in the
if-else chain, and will never hold here.
Linter – static analysis for Scala
String length approximation:
val msg = getMessage(arg)
...
if(msg.isEmpty) {
...
}
// So far so good...
Linter – static analysis for Scala
String length approximation:
val msg = getTimeStamp + getMessage(arg)
...
if(msg.isEmpty) {
...
}
Warning: This condition will never hold.
is Long, which means its
String length is from 1 to 20
Linter – static analysis for Scala
Detecting runtime exceptions early:
val foo = bar.replaceAll("?", ".")
Warning: Regex pattern syntax error:
Dangling meta character '?'
Example of Int abstract interpretation:
for (i ← 1 to 10) {
val div = 1/(i-1)
if(i > 15) ...
}
Warning: You will likely divide by zero here.
Warning: This condition will never hold.
Linter – static analysis for Scala
Many more checks
Unused sealed traits
Unused method
parameters
Pattern matching checks
Unnecessary ifs
“Suspicious” code
Linter – static analysis for Scala
How to get Linter?
Source code released under
the Apache License, ver 2.0
https://github.com/HairyFotr/linter
To use Linter in your project
follow the instructions there
Linter – static analysis for Scala
Future work
• Add more checks ← Everyone has ideas or a favorite bug
• Improve (or replace) abstract interpreters and
support more types (esp. Boolean, collections)
• Auto-generate parts of docs, config, and tests
(all three are incomplete at the moment)
Matic Potočnik - @HairyFotr
University of Ljubljana

More Related Content

Similar to Linter Static Analysis (Scala Workshop 2013)

Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
Antony Stubbs
 

Similar to Linter Static Analysis (Scala Workshop 2013) (20)

Mathematicians: Trust, but Verify
Mathematicians: Trust, but VerifyMathematicians: Trust, but Verify
Mathematicians: Trust, but Verify
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
NumPy/SciPy Statistics
NumPy/SciPy StatisticsNumPy/SciPy Statistics
NumPy/SciPy Statistics
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
 
Lazy java
Lazy javaLazy java
Lazy java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Jvm memory model
Jvm memory modelJvm memory model
Jvm memory model
 
sequencea.ppt
sequencea.pptsequencea.ppt
sequencea.ppt
 
Cat's anatomy
Cat's anatomyCat's anatomy
Cat's anatomy
 
Seminar on MATLAB
Seminar on MATLABSeminar on MATLAB
Seminar on MATLAB
 
PPT4: Frameworks & Libraries of Machine Learning & Deep Learning
PPT4: Frameworks & Libraries of Machine Learning & Deep Learning PPT4: Frameworks & Libraries of Machine Learning & Deep Learning
PPT4: Frameworks & Libraries of Machine Learning & Deep Learning
 
PPT - AutoML-Zero: Evolving Machine Learning Algorithms From Scratch
PPT - AutoML-Zero: Evolving Machine Learning Algorithms From ScratchPPT - AutoML-Zero: Evolving Machine Learning Algorithms From Scratch
PPT - AutoML-Zero: Evolving Machine Learning Algorithms From Scratch
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Linter Static Analysis (Scala Workshop 2013)

  • 1. Linter – static analysis for Scala Linter static analysis for Scala (Scala Workshop, 02.07.2013) Matic Potočnik - @HairyFotr University of Ljubljana
  • 2. Linter – static analysis for Scala What is Linter? Scala compiler plug-in for doing static analysis started by Jorge Ortiz Linter then... 6 checks Linter now... ~70 checks
  • 3. Linter – static analysis for Scala How does it work? Pattern matching on the Scala AST Abstract interpretation, mostly for Int and String
  • 4. Linter – static analysis for Scala Option and collection related checks: • Replace if(opt.isDefined) opt.get else alternative with opt.getOrElse(alternative) • Replace col.find(x => cond).isDefined with col.exists(x => cond) • Replace col.flatMap(x => if(cond) Some(x) else None) and col.flatMap(x => if(cond) List(x) else Nil) with col.filter(x => cond) • Using opt.size is practically always by mistake • ...
  • 5. Linter – static analysis for Scala Numeric math checks: • Loss of precision • Use log1p(x) instead of log(1 + x) • Use expm1(x) instead of exp(x) – 1 • BigDecimal(0.555555555555555555555) ... we get 0.5555555555555556 • Manually calculating functions like abs, signum, isNaN, ...
  • 6. Linter – static analysis for Scala Repeated part of condition: if(data.isEmpty && arg == “shortname” || arg == “name”) { ... } else if(arg == “shortname” || arg == “name”) { ... } else ... Warning: This condition has appeared earlier in the if-else chain, and will never hold here.
  • 7. Linter – static analysis for Scala String length approximation: val msg = getMessage(arg) ... if(msg.isEmpty) { ... } // So far so good...
  • 8. Linter – static analysis for Scala String length approximation: val msg = getTimeStamp + getMessage(arg) ... if(msg.isEmpty) { ... } Warning: This condition will never hold. is Long, which means its String length is from 1 to 20
  • 9. Linter – static analysis for Scala Detecting runtime exceptions early: val foo = bar.replaceAll("?", ".") Warning: Regex pattern syntax error: Dangling meta character '?' Example of Int abstract interpretation: for (i ← 1 to 10) { val div = 1/(i-1) if(i > 15) ... } Warning: You will likely divide by zero here. Warning: This condition will never hold.
  • 10. Linter – static analysis for Scala Many more checks Unused sealed traits Unused method parameters Pattern matching checks Unnecessary ifs “Suspicious” code
  • 11. Linter – static analysis for Scala How to get Linter? Source code released under the Apache License, ver 2.0 https://github.com/HairyFotr/linter To use Linter in your project follow the instructions there
  • 12. Linter – static analysis for Scala Future work • Add more checks ← Everyone has ideas or a favorite bug • Improve (or replace) abstract interpreters and support more types (esp. Boolean, collections) • Auto-generate parts of docs, config, and tests (all three are incomplete at the moment) Matic Potočnik - @HairyFotr University of Ljubljana