wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
let mut val=45; let r1=&mut val; let ref2=&mut val;
let mut val=45; {{ let r1=&mut val; }} let ref2=&mut val;
Only one mutable borrow.
Rust
if y > 88 puts 'output'
if y > 88 puts 'output' end
Add 'end'.
Ruby
if bar = 19 {{}}
if bar == 19 {{}}
Use ==.
Swift
fmt.Println 'data'
fmt.Println('data')
Missing parentheses.
Go
$data[43] = 5;
if (isset($data[43])) $data[43] = 5;
Check existence.
PHP
int[] list = new int[37]; list[37] = 5;
int[] list = new int[37]; if (37 < list.length) list[37] = 5;
Check bounds.
Java
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
with open('log.txt') as fh: data = fh.read()
with open('log.txt') as fh: data = fh.read()
Correct.
Python
if ($c = 10) {{}}
if ($c -eq 10) {{}}
Use -eq.
PowerShell
INSERT INTO orders VALUES ('world',21)
INSERT INTO orders (id, role) VALUES ('world',21);
Specify columns.
SQL
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
p {{ color: green }}
p {{ color: green; }}
Add semicolon.
CSS
var data int = 'world'
var data string = 'world'
Type mismatch.
Go
<?php // code ?>
<?php // code ?>
Correct.
PHP
h1 {{ font-size:52px color:green; }}
h1 {{ font-size:52px; color:green; }}
Add semicolon.
CSS
SELECT COUNT(*) FROM items
SELECT COUNT(*) FROM items;
Missing semicolon.
SQL
yield c
yield c
Correct yield.
Python
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
name: hello age: 14
name: hello age: 14
Correct.
YAML
<img src='test.jpg'>
<img src='test.jpg' alt='desc'>
Add alt text.
HTML
items[53]
if items.indices.contains(53) {{ items[53] }}
Check index.
Swift
if c > 13 print('result')
if c > 13: print('result')
Colon missing after if.
Python
{{'name':72, 'id' 55}}
{{'name':72, 'id':55}}
Colon missing.
Python
object User {{ def main(args: Array[String]) = println("data") }}
object User {{ def main(args: Array[String]): Unit = println("data") }}
Add return type Unit.
Scala
void main() {{ print('output') }}
void main() {{ print('output'); }}
Add semicolon.
Dart
'data' + 23
'data' + 23.to_s
Convert int.
Ruby
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
echo info test
echo 'info test'
Quote to prevent splitting.
Shell
let num: number = 'data';
let num: string = 'data';
Fix type.
TypeScript
if (count = 88) {{}}
if (count == 88) {{}}
Use ==.
Java
echo 'hello'
echo 'hello';
Add semicolon.
PHP
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
const person:Person = {{name:'result'}};
const person:Person = {{name:'result', age:91}};
Add missing property.
TypeScript
sys.sqrt(75)
import sys sys.sqrt(75)
Import module first.
Python
const result;
const result = 98;
Initialize const.
JavaScript
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(80);
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(80);
Correct.
Node.js
print 'output'
print('output')
print needs parentheses.
Python
System.out.println('info')
System.out.println('info');
Add semicolon.
Java
println('result')
println("result")
Double quotes.
Scala
my @arr = (100,11,24);
my @arr = (100,11,24);
Correct.
Perl
else print('hello')
else: print('hello')
Colon after else.
Python
'23' + 59
23 + 59
Avoid string coercion.
JavaScript
let result: i32 = "result";
let result: &str = "result";
Type mismatch.
Rust
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
class Item {{ int c; }};
class Item {{ public: int c; }};
Make public.
C++
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
if [ $index = 100 ]; then
if [ "$index" = 100 ]; then
Quote variable.
Shell
{{"name":"data" "value":13}}
{{"name":"data", "value":13}}
Add comma.
JSON
function baz(foo) print(foo) end
function baz(foo) print(foo) end
Correct.
Lua
[95, 9, 3
[95, 9, 3]
Close bracket.
Ruby
if (count = 64)
if (count == 64)
Use ==.
C++
def foo(): print('info')
def foo(): print('info')
Indent function body.
Python
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
def foo(item): return item + 1
def foo(item): return item + 1
Correct.
Python
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
data.forEach(function(a) {{ console.log(a); }})
data.forEach((a) => {{ console.log(a); }})
Arrow functions are cleaner.
JavaScript
int items[58]; items[58]=5;
int items[58]; if(58<58){{}} else items[58]=5;
Bounds check.
C++
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
<center>test</center>
<div style='text-align:center;'>test</div>
Use CSS.
HTML
if count = 51
if count == 51
Use ==.
Go
val data: Int = 'message'
val data: String = 'message'
Fix type.
Kotlin
{{'id':'test'}}
{{"id":"test"}}
Use double quotes.
JSON
switch(foo){{ case 61: break; }}
switch(foo){{ case 61: break; default: break; }}
Add default case.
Java
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
if (x = 15) {{}}
if (x == 15) {{}}
Use ==.
Kotlin
print 'value'
print 'value';
Add semicolon.
Perl
print 'hello'
print('hello')
Parentheses for function call.
Lua
<person age=56>
<person age="56">
Quote attribute.
XML
if (temp = 13) {{}}
if (temp === 13) {{}}
Use === for equality.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
function compute() {{ echo 'message'; }}
function compute() {{ echo 'message'; }}
Correct.
PHP
#footer {{ color: green; }}
#footer {{ color: green; }}
Correct.
CSS
for i=1,8 do print(i) end
for i=1,8 do print(i) end
Correct.
Lua
$bar = 28; if ($bar = 28) {{}}
$bar = 28; if ($bar == 28) {{}}
Use ==.
PHP
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
JOIN profiles ON items.id = profiles.email
JOIN profiles ON items.id = profiles.email
Correct.
SQL
let item: Int = 'output'
let item: String = 'output'
Fix type.
Swift
var x = 88;
var x = 88;
Correct.
Dart
<ul><li>test<li>data</ul>
<ul><li>test</li><li>data</li></ul>
Close li.
HTML
val val = 88; val = 91
var val = 88; val = 91
Use var for reassignment.
Scala
<input type='text' value='world'>
<input type='text' value='world' name='value'>
Add name attribute.
HTML
<table><tr><td>hello<td>data</tr></table>
<table><tr><td>hello</td><td>data</td></tr></table>
Close td.
HTML
const index = 47; index = 22;
let index = 47; index = 22;
Cannot reassign const.
JavaScript
<entry name='info'/>
<entry name="info"/>
Double quotes.
XML
fn test() -> i32 {{ 31 }}
fn test() -> i32 {{ 31 }}
Correct.
Rust
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
item = world
item = 'world'
Quote strings.
Python
while num > 94 num -= 1
while num > 94: num -= 1
Colon missing after while.
Python
List(25,3,79)
List(25,3,79)
Correct.
Scala
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
compute
compute()
Add parentheses.
Kotlin
if data = 23
if data == 23
Use ==.
Ruby
y > 15 & x < 31
y > 15 and x < 31
Use 'and' not '&'.
Python
match val {{ 1 => {{}} }}
match val {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust