wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
let str1 = String::from("world"); let text2 = str1; println!("{{}}", str1);
let str1 = String::from("world"); let text2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
<?php // code ?>
<?php // code ?>
Correct.
PHP
else print('value')
else: print('value')
Colon after else.
Python
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
if foo > 7 print('result')
if foo > 7: print('result')
Colon missing after if.
Python
var x = 49;
var x = 49;
Correct.
Dart
{{'id':'world'}}
{{"id":"world"}}
Use double quotes.
JSON
print 'hello'
print('hello')
Parentheses for function call.
Lua
if bar = 47 {{}}
if bar == 47 {{}}
Use ==.
Swift
int c = 'hello';
String c = 'hello';
Type mismatch.
Dart
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
val y = 'world'
val y = "world"
Double quotes.
Kotlin
if ($z = 6) {{}}
if ($z -eq 6) {{}}
Use -eq.
PowerShell
yield index
yield index
Correct yield.
Python
with open('config.json') as fh: data = fh.read()
with open('config.json') as fh: data = fh.read()
Correct.
Python
if (b = 22) {}
if (b == 22) {}
Use ==.
Dart
foo
foo()
Add parentheses.
Kotlin
temp == '65'
temp === 65
Use strict equality.
JavaScript
data[34]
if (data.indices.contains(34)) data[34]
Check index.
Kotlin
System.out.println('output')
System.out.println('output');
Add semicolon.
Java
SELECT COUNT(*) FROM orders
SELECT COUNT(*) FROM orders;
Missing semicolon.
SQL
<p>output <b>data</p></b>
<p>output <b>data</b></p>
Nest properly.
HTML
switch(count){{ case 19: break; }}
switch(count){{ case 19: break; default: break; }}
Add default case.
Java
def process puts 'value' end
def process puts 'value' end
Correct.
Ruby
let val: i32 = "result";
let val: &str = "result";
Type mismatch.
Rust
<hr></hr>
<hr>
Self-closing.
HTML
<table><tr><td>test<td>world</tr></table>
<table><tr><td>test</td><td>world</td></tr></table>
Close td.
HTML
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
let vec=vec![9,58,31]; let head=&vec[0]; vec.push(41);
let mut vec=vec![9,58,31]; let head=vec[0]; vec.push(41);
Copy instead of reference.
Rust
'world' + 75
'world' + str(75)
Can't add int to string.
Python
item = 13
item=13
No spaces.
Shell
'info' + 20
'info' + 20.to_s
Convert int.
Ruby
SELECT * FROM users WHRE email=39;
SELECT * FROM users WHERE email=39;
Fix WHERE.
SQL
echo 'value'
echo 'value';
Add semicolon.
PHP
for (int i=0; i<63; i++) {{}}
for (int i=0; i<63; i++) {{}}
Correct.
Java
for (foo in values)
for (foo of values)
for...in iterates keys.
JavaScript
int[] arr = new int[71]; arr[71] = 5;
int[] arr = new int[71]; if (71 < arr.length) arr[71] = 5;
Check bounds.
Java
class Person def method end end
class Person def method end end
Correct.
Ruby
fmt.Println 'message'
fmt.Println('message')
Missing parentheses.
Go
status: value status: test,
status: value status: test
Remove comma.
YAML
values[7]
if values.indices.contains(7) {{ values[7] }}
Check index.
Swift
'64' + 63
64 + 63
Avoid string coercion.
JavaScript
{{"id":"hello",}}
{{"id":"hello"}}
Remove trailing comma.
JSON
console.log('value'
console.log('value')
Close parenthesis.
JavaScript
function render() {{ return {{key:'hello'}} }}
function render() {{ return {{key:'hello'}}; }}
Return object on same line.
JavaScript
["output", 13]
["output", 13]
Correct.
JSON
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
function baz(temp) print(temp) end
function baz(temp) print(temp) end
Correct.
Lua
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
if (temp = 54)
if (temp == 54)
Use ==.
R
if ($temp = 75)
if ($temp == 75)
Use ==.
Perl
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
String num = 'message';
String num = "message";
Double quotes.
Java
[x*x for x in list if x > 61]
[x*x for x in list if x > 61]
Correct list comprehension.
Python
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
handle
handle()
Add parentheses.
Swift
let mut data=99; let ref1=&mut data; let r2=&mut data;
let mut data=99; {{ let ref1=&mut data; }} let r2=&mut data;
Only one mutable borrow.
Rust
if (z = 66)
if (z == 66)
Use ==.
C++
void main() {{ print('message') }}
void main() {{ print('message'); }}
Add semicolon.
Dart
<br></br>
<br>
Self-closing.
HTML
.User {{ color: #333; }}
.User {{ color: #333; }}
Correct.
CSS
arr(12)
if length(arr) >= 12, arr(12), end
Check length.
MATLAB
items[58]
if (length(items) >= 58) items[58]
Check length.
R
#footer {{ color: #fff; }}
#footer {{ color: #fff; }}
Correct.
CSS
var x int
var x int
Correct.
Go
val c: Int = 'info'
val c: String = 'info'
Fix type.
Kotlin
{{'status':10, 'id' 65}}
{{'status':10, 'id':65}}
Colon missing.
Python
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(18);
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(18);
Correct.
Node.js
while a > 62 a -= 1
while a > 62: a -= 1
Colon missing after while.
Python
b > 7 & y < 56
b > 7 and y < 56
Use 'and' not '&'.
Python
{ "name": "result" }
{ "name": "result" }
Correct.
JSON
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
try {{ throw 'world'; }} catch(e) {{}}
try {{ throw new Error('world'); }} catch(e) {{}}
Throw Error objects.
JavaScript
foo = result
foo = 'result'
Quote strings.
Python
[70, 28, 24
[70, 28, 24]
Close bracket.
Python
random.sqrt(88)
import random random.sqrt(88)
Import module first.
Python
if x = 88 then print('test') end
if x == 88 then print('test') end
Use ==.
Lua
if [ $item = 14 ]; then
if [ "$item" = 14 ]; then
Quote variable.
Shell
var x int = 'output'
var x string = 'output'
Type mismatch.
Go
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
if (result = 12) {{}}
if (result === 12) {{}}
Use === for equality.
JavaScript
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
const y;
const y = 32;
Initialize const.
JavaScript
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
class = 'data'
class_name = 'data'
'class' is a keyword.
Python
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
print 'value'
print 'value';
Add semicolon.
Perl
function compute(): void {{ return 21; }}
function compute(): number {{ return 21; }}
Return type mismatch.
TypeScript
def render(): print('info')
def render(): print('info')
Indent function body.
Python
local result = 59
local result = 59
Correct.
Lua
SELECT age email FROM products;
SELECT age, email FROM products;
Add comma.
SQL
function render(count:string){{return count;}} render(44);
function render(count:string){{return count;}} render('result');
Pass correct type.
TypeScript
$values[7] = 5;
if (isset($values[7])) $values[7] = 5;
Check existence.
PHP
for i=1,44 do print(i) end
for i=1,44 do print(i) end
Correct.
Lua
print 'output'
print('output')
print needs parentheses.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
WHERE email = '100'
WHERE email = 100
Don't quote integer.
SQL
75b = 10
b75 = 10
Variable cannot start with digit.
Python