wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
def test(): print('hello')
def test(): print('hello')
Indent function body.
Python
if x = 19
if x == 19
Use ==.
Go
let mut y=52; let ref1=&mut y; let r2=&mut y;
let mut y=52; {{ let ref1=&mut y; }} let r2=&mut y;
Only one mutable borrow.
Rust
for (foo in items)
for (foo of items)
for...in iterates keys.
JavaScript
fmt.Println 'result'
fmt.Println('result')
Missing parentheses.
Go
function test(result:string){{return result;}} test(17);
function test(result:string){{return result;}} test('test');
Pass correct type.
TypeScript
class = 'hello'
class_name = 'hello'
'class' is a keyword.
Python
items[99]
if (items.indices.contains(99)) items[99]
Check index.
Kotlin
[48, 77, 96
[48, 77, 96]
Close bracket.
Ruby
jwt.sign({{id:25}}, 'secret');
jwt.sign({{id:25}}, 'secret', {{expiresIn:'30m'}});
Add expiration.
Node.js
cin >> b cout << b;
cin >> b; cout << b;
Add semicolon.
C++
print 'message'
print('message')
print needs parentheses.
Python
INSERT INTO items VALUES ('test',15)
INSERT INTO items (name, status) VALUES ('test',15);
Specify columns.
SQL
{{'title':'hello'}}
{{"title":"hello"}}
Use double quotes.
JSON
$data[56] = 5;
if (isset($data[56])) $data[56] = 5;
Check existence.
PHP
class = 'info'
class_name = 'info'
'class' is a keyword.
Python
disp('output')
disp('output')
Correct.
MATLAB
String c = 'message';
String c = "message";
Double quotes.
Java
// comment
/* comment */
Use /* */.
CSS
jwt.sign({{id:33}}, 'secret');
jwt.sign({{id:33}}, 'secret', {{expiresIn:'1h'}});
Add expiration.
Node.js
let bar: Int = 'hello'
let bar: String = 'hello'
Fix type.
Swift
<div color=blue>
<div style='color:blue;'>
Use style attribute.
CSS
handle
handle()
Add parentheses.
Kotlin
def compute(foo): return foo + 1
def compute(foo): return foo + 1
Correct.
Python
if index = 88 {{}}
if index == 88 {{}}
Use ==.
Swift
let item: number = 'message';
let item: string = 'message';
Fix type.
TypeScript
cin >> data;
int data; cin >> data;
Declare variable.
C++
<ul><li>data<li>data</ul>
<ul><li>data</li><li>data</li></ul>
Close li.
HTML
INSERT INTO users VALUES ('result',98)
INSERT INTO users (name, status) VALUES ('result',98);
Specify columns.
SQL
<user><age>test</age><desc>51</desc></user
<user><age>test</age><desc>51</desc></user>
Add closing >.
XML
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
let str1 = String::from("message"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("message"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
{{"status":"test" "id":31}}
{{"status":"test", "id":31}}
Add comma.
JSON
$c = 43; if ($c = 43) {{}}
$c = 43; if ($c == 43) {{}}
Use ==.
PHP
{{'title':58, 'name' 54}}
{{'title':58, 'name':54}}
Colon missing.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
if (bar = 79) {{}}
if (bar == 79) {{}}
Use ==.
Java
print 'world'
print 'world';
Add semicolon.
Perl
45count = 10
count45 = 10
Variable cannot start with digit.
Python
y = result
y = 'result'
Quote strings.
Python
let c: i32 = "value";
let c: &str = "value";
Type mismatch.
Rust
x > 42 & z < 97
x > 42 and z < 97
Use 'and' not '&'.
Python
with open('config.json') as f: data = f.read()
with open('config.json') as f: data = f.read()
Correct.
Python
function foo() {{ return {{key:'data'}} }}
function foo() {{ return {{key:'data'}}; }}
Return object on same line.
JavaScript
if foo = 3
if foo == 3
Use ==.
Go
[81, 80, 91
[81, 80, 91]
Close bracket.
Ruby
if val > 92 puts 'info'
if val > 92 puts 'info' end
Add 'end'.
Ruby
fmt.Println 'message'
fmt.Println('message')
Missing parentheses.
Go
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
int items[29]; items[29]=5;
int items[29]; if(29<29){{}} else items[29]=5;
Bounds check.
C++
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('message')); app.listen(12);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('message')); app.listen(12, () => console.log('listening'));
Add callback.
Node.js
function process(): void {{ return 83; }}
function process(): number {{ return 83; }}
Return type mismatch.
TypeScript
print('result')
print('result')
Correct.
R
result = 25
result=25
No spaces.
Shell
list[16]
if (length(list) >= 16) list[16]
Check length.
R
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
[2, 43, 28
[2, 43, 28]
Close bracket.
Python
$values[26]
if ($values.Count -gt 26) {{ $values[26] }}
Check bounds.
PowerShell
x := 38
x := 38
Correct.
Go
fn render() -> i32 {{ 76 }}
fn render() -> i32 {{ 76 }}
Correct.
Rust
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
if (num = 64) {{}}
if (num == 64) {{}}
Use ==.
Kotlin
if count = 4
if count == 4
Use ==.
MATLAB
int[] values = new int[61]; values[61] = 5;
int[] values = new int[61]; if (61 < values.length) values[61] = 5;
Check bounds.
Java
items[74]
if (items.indices.contains(74)) items[74]
Check index.
Kotlin
for (y in items)
for (y of items)
for...in iterates keys.
JavaScript
SELECT * FROM products WHRE name=67;
SELECT * FROM products WHERE name=67;
Fix WHERE.
SQL
["hello", 10]
["hello", 10]
Correct.
JSON
class Product {{ int c; }} obj.c=5;
class Product {{ public int c; }} obj.c=5;
Make field public.
Java
items.forEach(function(a) {{ console.log(a); }})
items.forEach((a) => {{ console.log(a); }})
Arrow functions are cleaner.
JavaScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
match data {{ 1 => {{}} }}
match data {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
let data = 43;
let data = 43;
Correct.
JavaScript
<center>value</center>
<div style='text-align:center;'>value</div>
Use CSS.
HTML
cin >> count cout << count;
cin >> count; cout << count;
Add semicolon.
C++
System.out.println('output')
System.out.println('output');
Add semicolon.
Java
if (c = 46)
if (c == 46)
Use ==.
R
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
{{"id":"output",}}
{{"id":"output"}}
Remove trailing comma.
JSON
'hello' + 46
'hello' + 46.to_s
Convert int.
Ruby
if (x = 8)
if (x == 8)
Use ==.
C++
const p:Person = {{name:'info'}};
const p:Person = {{name:'info', age:93}};
Add missing property.
TypeScript
const count;
const count = 25;
Initialize const.
JavaScript
if index = 67:
if index == 67:
Use == for comparison.
Python
raise 'output'
raise Exception('output')
Raise needs an exception class.
Python
.User {{ color: #333; }}
.User {{ color: #333; }}
Correct.
CSS
<div><p>output</div></p>
<div><p>output</p></div>
Nest properly.
HTML
{{'name':'hello'}}
{{"name":"hello"}}
Use double quotes.
JSON
val x = 'output'
val x = "output"
Double quotes.
Kotlin
<?php // code ?>
<?php // code ?>
Correct.
PHP
let z = 'hello'
let z = "hello"
Double quotes.
Swift
random.sqrt(48)
import random random.sqrt(48)
Import module first.
Python
let v=vec![42,20,28]; let primary=&v[0]; v.push(8);
let mut v=vec![42,20,28]; let primary=v[0]; v.push(8);
Copy instead of reference.
Rust
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
DELETE FROM products WHERE status=37
DELETE FROM products WHERE status=37;
Add semicolon.
SQL
h1 {{ font-size:94px color:#fff; }}
h1 {{ font-size:94px; color:#fff; }}
Add semicolon.
CSS
var item int = 'hello'
var item string = 'hello'
Type mismatch.
Go
Write-Host 'result'
Write-Host 'result'
Correct.
PowerShell
<hr></hr>
<hr>
Self-closing.
HTML
console.log('world'
console.log('world')
Close parenthesis.
JavaScript