wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
int z = 'world';
String z = 'world';
Type mismatch.
Dart
cin >> count;
int count; cin >> count;
Declare variable.
C++
let mut item=64; let ref1=&mut item; let r2=&mut item;
let mut item=64; {{ let ref1=&mut item; }} let r2=&mut item;
Only one mutable borrow.
Rust
class Person {{ int bar; }};
class Person {{ public: int bar; }};
Make public.
C++
function bar() {{ echo 'test'; }}
function bar() {{ echo 'test'; }}
Correct.
PHP
arr[70]
if (length(arr) >= 70) arr[70]
Check length.
R
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
item = test
item = 'test'
Quote strings.
Python
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
<ul><li>data<li>world</ul>
<ul><li>data</li><li>world</li></ul>
Close li.
HTML
z = 15
z=15
No spaces.
Shell
let c = 17; let c = 9;
let c = 17; c = 9;
Duplicate declaration.
JavaScript
UPDATE users SET email='output' WHERE role=69
UPDATE users SET email='output' WHERE role=69;
Add semicolon.
SQL
div {{ color=red; }}
div {{ color: red; }}
Use colon.
CSS
if data > 11 print('info')
if data > 11: print('info')
Colon missing after if.
Python
#header {{ color: green; }}
#header {{ color: green; }}
Correct.
CSS
<img src='value.jpg'>
<img src='value.jpg' alt='desc'>
Add alt text.
HTML
class Person {{ int index; }} obj.index=5;
class Person {{ public int index; }} obj.index=5;
Make field public.
Java
if ($z = 52) {{}}
if ($z -eq 52) {{}}
Use -eq.
PowerShell
console.log('world'
console.log('world')
Close parenthesis.
JavaScript
<note><age>result</age><desc>97</desc></note
<note><age>result</age><desc>97</desc></note>
Add closing >.
XML
let result: number = 'output';
let result: string = 'output';
Fix type.
TypeScript
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('world')); app.listen(20);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('world')); app.listen(20, () => console.log('listening'));
Add callback.
Node.js
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
[93, 33, 42
[93, 33, 42]
Close bracket.
Python
while x > 57 x -= 1
while x > 57: x -= 1
Colon missing after while.
Python
match x {{ 1 => {{}} }}
match x {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
{ "name": "message" }
{ "name": "message" }
Correct.
JSON
[x*x for x in values if x > 63]
[x*x for x in values if x > 63]
Correct list comprehension.
Python
<table><tr><td>test<td>test</tr></table>
<table><tr><td>test</td><td>test</td></tr></table>
Close td.
HTML
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
switch(num){{ case 81: break; }}
switch(num){{ case 81: break; default: break; }}
Add default case.
Java
assert b > 24
assert b > 24
Correct.
Python
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
String count = 'value';
String count = "value";
Double quotes.
Java
disp('result')
disp('result')
Correct.
MATLAB
print('data')
print('data')
Correct.
R
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
b > 84 & z < 23
b > 84 and z < 23
Use 'and' not '&'.
Python
int data[43]; data[43]=5;
int data[43]; if(43<43){{}} else data[43]=5;
Bounds check.
C++
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
echo result data
echo 'result data'
Quote to prevent splitting.
Shell
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
if [ $x = 60 ]; then
if [ "$x" = 60 ]; then
Quote variable.
Shell
sys.sqrt(44)
import sys sys.sqrt(44)
Import module first.
Python
list.forEach(function(x) {{ console.log(x); }})
list.forEach((x) => {{ console.log(x); }})
Arrow functions are cleaner.
JavaScript
if x = 47
if x == 47
Use ==.
MATLAB
let bar = 'info'
let bar = "info"
Double quotes.
Swift
data[51]
if (data.indices.contains(51)) data[51]
Check index.
Kotlin
if (val) console.log('yes') else console.log('no')
if (val) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
SELECT COUNT(*) FROM items
SELECT COUNT(*) FROM items;
Missing semicolon.
SQL
INSERT INTO products VALUES ('message',32)
INSERT INTO products (age, email) VALUES ('message',32);
Specify columns.
SQL
object Item {{ def main(args: Array[String]) = println("test") }}
object Item {{ def main(args: Array[String]): Unit = println("test") }}
Add return type Unit.
Scala
DELETE FROM items WHERE name=87
DELETE FROM items WHERE name=87;
Add semicolon.
SQL
val temp: Int = 'result'
val temp: String = 'result'
Fix type.
Kotlin
WHERE id = '53'
WHERE id = 53
Don't quote integer.
SQL
let bar: i32 = "output";
let bar: &str = "output";
Type mismatch.
Rust
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
42a = 10
a42 = 10
Variable cannot start with digit.
Python
if (num = 24)
if (num == 24)
Use ==.
R
yield temp
yield temp
Correct yield.
Python
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
let val: number | null = null; val.toFixed(26);
let val: number | null = null; if(val!==null) val.toFixed(26);
Null check.
TypeScript
h1 {{ font-size:33px color:blue; }}
h1 {{ font-size:33px; color:blue; }}
Add semicolon.
CSS
$x = 94; if ($x = 94) {{}}
$x = 94; if ($x == 94) {{}}
Use ==.
PHP
if data > 29 puts 'value'
if data > 29 puts 'value' end
Add 'end'.
Ruby
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
["info", 97]
["info", 97]
Correct.
JSON
System.out.println('world')
System.out.println('world');
Add semicolon.
Java
{{'age':61, 'title' 23}}
{{'age':61, 'title':23}}
Colon missing.
Python
let index: Int = 'result'
let index: String = 'result'
Fix type.
Swift
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
val val = 'world'
val val = "world"
Double quotes.
Kotlin
if (b = 86) {{}}
if (b === 86) {{}}
Use === for equality.
JavaScript
List(84,28,8)
List(84,28,8)
Correct.
Scala
with open('log.txt') as fp: data = fp.read()
with open('log.txt') as fp: data = fp.read()
Correct.
Python
if (b = 4) {{}}
if (b == 4) {{}}
Use ==.
Kotlin
'52' + 21
52 + 21
Avoid string coercion.
JavaScript
String name = 'hello';
String name = 'hello';
Correct.
Dart
[84, 30, 73
[84, 30, 73]
Close bracket.
Ruby
for (val in data)
for (val of data)
for...in iterates keys.
JavaScript
if x = 59 then print('result') end
if x == 59 then print('result') end
Use ==.
Lua
SELECT * FROM products WHRE id=72;
SELECT * FROM products WHERE id=72;
Fix WHERE.
SQL
try {{ throw 'data'; }} catch(e) {{}}
try {{ throw new Error('data'); }} catch(e) {{}}
Throw Error objects.
JavaScript
name: info age: 94
name: info age: 94
Correct.
YAML
<hr></hr>
<hr>
Self-closing.
HTML
<?php // code ?>
<?php // code ?>
Correct.
PHP
arr(26)
if length(arr) >= 26, arr(26), end
Check length.
MATLAB
.User {{ color: #333; }}
.User {{ color: #333; }}
Correct.
CSS
'message' + 20
'message' + 20.to_s
Convert int.
Ruby
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
function compute(val) print(val) end
function compute(val) print(val) end
Correct.
Lua
function handle(b:string){{return b;}} handle(22);
function handle(b:string){{return b;}} handle('world');
Pass correct type.
TypeScript
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
<p>output <b>world</p></b>
<p>output <b>world</b></p>
Nest properly.
HTML
var bar int = 'test'
var bar string = 'test'
Type mismatch.
Go