-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscala-linting.html
203 lines (161 loc) · 4.64 KB
/
scala-linting.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
<title>Scala Linting</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
.remark-inline-code {
background-color: #F0F0F0;
padding: 4px;
font-size: 16px;
border-radius: 2px;
}
.footnote {
margin-top: 100px;
font-size: 18px;
opacity: 0.7;
}
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Scala Linting at AS24
.footnote[
Bayram Kiran
Software Engineer @ Cxp Engagement Web
]
---
## Prevent using operations which throw exceptions
`Option#get` throws an exception when the value is None
```scala
@ val name: Option[String] = None
name: Option[String] = None
@ name.get
java.util.NoSuchElementException: None.get
scala.None$.get(Option.scala:627)
scala.None$.get(Option.scala:626)
ammonite.$sess.cmd19$.<clinit>(cmd19.sc:1)
```
**Detect with:** `WartRemover#OptionPartial`
---
## Prevent using operations which throw exceptions
```scala
@ val names: List[String] = List.empty
names: List[String] = List()
@ names.head
java.util.NoSuchElementException: head of empty list
scala.collection.immutable.Nil$.head(List.scala:592)
scala.collection.immutable.Nil$.head(List.scala:591)
ammonite.$sess.cmd21$.<clinit>(cmd21.sc:1)
```
--
`scala.collection.Traversable` has a lot of methods like:
```scala
Traversable#(head, tail, last, reduce, reduceLeft, min, max, minBy, maxBy, ...)
```
which will throw exception when the collection is empty<br/>
**Detect with:** `WartRemover#TraversableOps`
<hr/>
--
**And more:** `Try#get`, `Either#LeftProjection#get`, `Either#RightProjection#get`, `asInstanceOf`
---
## Detect possible bugs at compile time
```scala
@ def rate(occurrence: Int, total: Int): Double = occurrence / total
defined function rate
@ rate(10, 100)
res0: Double = 0.0
```
**Detect with:** `-Wnumeric-widen`
<hr/>
--
```scala
Array(1) == Array(1) //this returns false, use sameElements instead
```
**Detect with:** `WartRemover#ArrayEquals`
<hr/>
--
Do not use `+` function to concatenate strings
```scala
Some("foo") + "bar" //returns "Some(foo)bar"
Set("foo") + "bar" //returns Set("foo", "bar")
```
**Detect with:** `WartRemover#StringPlusAny`
---
## Keep your codebase clean and up-to-date
`-Wdead-code`<br>
Warn when dead code is identified.
<hr/>
`-opt:unreachable-code`<br>
Eliminate unreachable code, exception handlers guarding no instructions, redundant metadata (debug information, line numbers).
<hr/>
`-Xlint:unused`<br>
Enable -Wunused:imports,privates,locals,implicits.
<hr/>
`-Xlint:deprecation`<br>
Emit warning and location for usages of deprecated APIs
---
## How to use?
We have already setup `ScoutLintingPlugin` (provides compiler flags) and `ScoutWartRemoverPlugin` (since v0.2.1) in `sbt-scalascout` with a default set of flags/rules.
--
1. Add `sbt-scalascout` dependency to your `plugins.sbt` file:
```scala
addSbtPlugin("com.autoscout24" % "sbt-scalascout" % "0.2.2")
```
--
2. Enable the plugins in your `build.sbt` file like:
```scala
lazy val root = project
.in(file("."))
.enablePlugins(ScoutLintingPlugin, ScoutWartRemoverPlugin)
```
--
That's it, you're ready to go! :)
**P.S.** Both plugins are enabled for all `scala` branches in `as24-project-template`
---
## How to disable warnings
--
For WartRemover, to disable a single line:
```scala
@SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.Null"))
var foo = null
```
For compiler warnings, to disable a single line:
```scala
@silent
val abc = "this is ${result.code}"
```
<hr/>
--
For WartRemover, to disable a file or folder:
```scala
wartremoverExcluded += baseDirectory.value / "app" / "SomeFile.scala"
wartremoverExcluded += baseDirectory.value / "app" / "some_folder"
```
For compiler warnings, to disable a file or folder:
```scala
scalacOptions += "-P:silencer:pathFilters=app/SomeFile.scala"
scalacOptions += "-P:silencer:pathFilters=app/some_folder"
```
---
class: center, middle
## Thank you for your attention :)
## Any questions?
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>