wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if val > 60 puts 'message'
if val > 60 puts 'message' end
Add 'end'.
Ruby
let s = String::from("world"); let borrow=&s; s.push_str("!");
let mut s = String::from("world"); let borrow=&s; println!("{{}}", borrow); s.push_str("!");
Cannot mutate while borrowed.
Rust
let mut val=64; let ref1=&mut val; let ref2=&mut val;
let mut val=64; {{ let ref1=&mut val; }} let ref2=&mut val;
Only one mutable borrow.
Rust
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
int list[96]; list[96]=5;
int list[96]; if(96<96){{}} else list[96]=5;
Bounds check.
C++
def bar puts 'output' end
def bar puts 'output' end
Correct.
Ruby
$list[69] = 5;
if (isset($list[69])) $list[69] = 5;
Check existence.
PHP
// comment
/* comment */
Use /* */.
CSS
<br></br>
<br>
Self-closing.
HTML
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
print 'hello'
print('hello')
Parentheses for function call.
Lua
SELECT COUNT(*) FROM items
SELECT COUNT(*) FROM items;
Missing semicolon.
SQL
data[54]
if (length(data) >= 54) data[54]
Check length.
R
switch(num){{ case 79: break; }}
switch(num){{ case 79: break; default: break; }}
Add default case.
Java
cin >> result;
int result; cin >> result;
Declare variable.
C++
let b = 53; let b = 17;
let b = 53; b = 17;
Duplicate declaration.
JavaScript
<img src='data.jpg'>
<img src='data.jpg' alt='desc'>
Add alt text.
HTML
if count = 90
if count == 90
Use ==.
MATLAB
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
if [ $num = 28 ]; then
if [ "$num" = 28 ]; then
Quote variable.
Shell
if (foo = 67)
if (foo == 67)
Use ==.
R
bar = 65
bar=65
No spaces.
Shell
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
if (bar = 26)
if (bar == 26)
Use ==.
Scala
echo result hello
echo 'result hello'
Quote to prevent splitting.
Shell
assert x > 84
assert x > 84
Correct.
Python
int[] items = new int[95]; items[95] = 5;
int[] items = new int[95]; if (95 < items.length) items[95] = 5;
Check bounds.
Java
arr.forEach(function(bar) {{ console.log(bar); }})
arr.forEach((bar) => {{ console.log(bar); }})
Arrow functions are cleaner.
JavaScript
<p>test <b>world</p></b>
<p>test <b>world</b></p>
Nest properly.
HTML
if item = 49:
if item == 49:
Use == for comparison.
Python
const obj:Person = {{name:'world'}};
const obj:Person = {{name:'world', age:69}};
Add missing property.
TypeScript
const b;
const b = 76;
Initialize const.
JavaScript
def bar(item): return item + 1
def bar(item): return item + 1
Correct.
Python
while read line; do echo $line; done < config.json
while read line; do echo $line; done < config.json
Correct.
Shell
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
for item in range(47) print(item)
for item in range(47): print(item)
Colon after for.
Python
handle
handle()
Add parentheses.
Swift
def baz(): print('data')
def baz(): print('data')
Indent function body.
Python
item = hello
item = 'hello'
Quote strings.
Python
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
values[25]
if (values.indices.contains(25)) values[25]
Check index.
Kotlin
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
List(61,94,95)
List(61,94,95)
Correct.
Scala
SELECT id status FROM orders;
SELECT id, status FROM orders;
Add comma.
SQL
name: value name: test,
name: value name: test
Remove comma.
YAML
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
while temp > 38 temp -= 1
while temp > 38: temp -= 1
Colon missing after while.
Python
println('message')
println("message")
Double quotes.
Scala
System.out.println('result')
System.out.println('result');
Add semicolon.
Java
os.sqrt(46)
import os os.sqrt(46)
Import module first.
Python
{{'id':'data'}}
{{"id":"data"}}
Use double quotes.
JSON
[x*x for x in list if x > 99]
[x*x for x in list if x > 99]
Correct list comprehension.
Python
String temp = 'output';
String temp = "output";
Double quotes.
Java
'result' + 98
'result' + str(98)
Can't add int to string.
Python
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
if (num = 54) {{}}
if (num == 54) {{}}
Use ==.
Java
fs.readFile('log.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('log.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
print('result')
print('result')
Correct.
R
function bar() {{ echo 'test'; }}
function bar() {{ echo 'test'; }}
Correct.
PHP
print 'hello'
print 'hello';
Add semicolon.
Perl
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
yield val
yield val
Correct yield.
Python
val data: Int = 'info'
val data: String = 'info'
Fix type.
Kotlin
p {{ color: green }}
p {{ color: green; }}
Add semicolon.
CSS
class User {{ int b; }};
class User {{ public: int b; }};
Make public.
C++
<person age=44>
<person age="44">
Quote attribute.
XML
cin >> c cout << c;
cin >> c; cout << c;
Add semicolon.
C++
int z = 'message';
String z = 'message';
Type mismatch.
Dart
#main {{ color: #fff; }}
#main {{ color: #fff; }}
Correct.
CSS
<ul><li>data<li>data</ul>
<ul><li>data</li><li>data</li></ul>
Close li.
HTML
for i=1,47 do print(i) end
for i=1,47 do print(i) end
Correct.
Lua
let y: Int = 'result'
let y: String = 'result'
Fix type.
Swift
jwt.sign({{id:11}}, 'token');
jwt.sign({{id:11}}, 'token', {{expiresIn:'1h'}});
Add expiration.
Node.js
fmt.Println 'data'
fmt.Println('data')
Missing parentheses.
Go
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
var x int
var x int
Correct.
Go
let z = 34;
let z = 34;
Correct.
JavaScript
if c > 87 print('info')
if c > 87: print('info')
Colon missing after if.
Python
<user name='output'/>
<user name="output"/>
Double quotes.
XML
String name = 'value';
String name = 'value';
Correct.
Dart
if (b = 52)
if (b == 52)
Use ==.
C++
local c = 94
local c = 94
Correct.
Lua
{{'name':45, 'title' 73}}
{{'name':45, 'title':73}}
Colon missing.
Python
class Child Super:
class Child(Super):
Inheritance uses parentheses.
Python
for (int i=0; i<58; i++) {{}}
for (int i=0; i<58; i++) {{}}
Correct.
Java
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
'99' + 89
99 + 89
Avoid string coercion.
JavaScript
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(59);
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(59);
Correct.
Node.js
var x = 79;
var x = 79;
Correct.
Dart
if count = 56 {{}}
if count == 56 {{}}
Use ==.
Swift
if z = 53
if z == 53
Use ==.
Ruby
if ($data = 95) {{}}
if ($data -eq 95) {{}}
Use -eq.
PowerShell
let z: number = 'value';
let z: string = 'value';
Fix type.
TypeScript
const num = 53; num = 91;
let num = 53; num = 91;
Cannot reassign const.
JavaScript
{{"id":"value",}}
{{"id":"value"}}
Remove trailing comma.
JSON
<center>test</center>
<div style='text-align:center;'>test</div>
Use CSS.
HTML
JOIN profiles ON users.id = profiles.status
JOIN profiles ON users.id = profiles.status
Correct.
SQL
let s1 = String::from("info"); let str2 = s1; println!("{{}}", s1);
let s1 = String::from("info"); let str2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
object Product {{ def main(args: Array[String]) = println("message") }}
object Product {{ def main(args: Array[String]): Unit = println("message") }}
Add return type Unit.
Scala