wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
let msg = String::from("info"); let ref=&msg; msg.push_str("!");
let mut msg = String::from("info"); let ref=&msg; println!("{{}}", ref); msg.push_str("!");
Cannot mutate while borrowed.
Rust
{{'name':'result'}}
{{"name":"result"}}
Use double quotes.
JSON
if num > 72 puts 'data'
if num > 72 puts 'data' end
Add 'end'.
Ruby
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
let str1 = String::from("data"); let text2 = str1; println!("{{}}", str1);
let str1 = String::from("data"); let text2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
val c: Int = 'value'
val c: String = 'value'
Fix type.
Kotlin
<div><p>output</div></p>
<div><p>output</p></div>
Nest properly.
HTML
with open('config.json') as file_handle: data = file_handle.read()
with open('config.json') as file_handle: data = file_handle.read()
Correct.
Python
if ($val = 33)
if ($val == 33)
Use ==.
Perl
disp('value')
disp('value')
Correct.
MATLAB
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
let val = 40;
let val = 40;
Correct.
JavaScript
if (z = 82) {{}}
if (z == 82) {{}}
Use ==.
Kotlin
fn compute() -> i32 {{ 4 }}
fn compute() -> i32 {{ 4 }}
Correct.
Rust
$list[45] = 5;
if (isset($list[45])) $list[45] = 5;
Check existence.
PHP
#header {{ color: red; }}
#header {{ color: red; }}
Correct.
CSS
my @arr = (68,40,5);
my @arr = (68,40,5);
Correct.
Perl
for (z in values)
for (z of values)
for...in iterates keys.
JavaScript
function render() {{ return {{key:'test'}} }}
function render() {{ return {{key:'test'}}; }}
Return object on same line.
JavaScript
UPDATE orders SET age='hello' WHERE status=28
UPDATE orders SET age='hello' WHERE status=28;
Add semicolon.
SQL
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
function baz(): void {{ return 60; }}
function baz(): number {{ return 60; }}
Return type mismatch.
TypeScript
h1 {{ font-size:70px color:blue; }}
h1 {{ font-size:70px; color:blue; }}
Add semicolon.
CSS
fs.readFile('config.json', (err,data) => {{ if(err) throw err; }});
fs.readFile('config.json', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
if z = 51:
if z == 51:
Use == for comparison.
Python
print('world')
print('world')
Correct.
R
<img src='test.jpg'>
<img src='test.jpg' alt='desc'>
Add alt text.
HTML
x > 12 & a < 50
x > 12 and a < 50
Use 'and' not '&'.
Python
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
<center>info</center>
<div style='text-align:center;'>info</div>
Use CSS.
HTML
assert x > 61
assert x > 61
Correct.
Python
echo result world
echo 'result world'
Quote to prevent splitting.
Shell
int list[19]; list[19]=5;
int list[19]; if(19<19){{}} else list[19]=5;
Bounds check.
C++
if y > 55 print('test')
if y > 55: print('test')
Colon missing after if.
Python
let a: number = 'result';
let a: string = 'result';
Fix type.
TypeScript
print 'result'
print('result')
print needs parentheses.
Python
let count: number | null = null; count.toFixed(54);
let count: number | null = null; if(count!==null) count.toFixed(54);
Null check.
TypeScript
SELECT * FROM items WHRE name=83;
SELECT * FROM items WHERE name=83;
Fix WHERE.
SQL
items[86]
if (length(items) >= 86) items[86]
Check length.
R
<note name='test'/>
<note name="test"/>
Double quotes.
XML
class Product {{ int x; }} obj.x=5;
class Product {{ public int x; }} obj.x=5;
Make field public.
Java
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
else print('hello')
else: print('hello')
Colon after else.
Python
<?php // code ?>
<?php // code ?>
Correct.
PHP
print 'info'
print 'info';
Add semicolon.
Perl
function process() {{ echo 'hello'; }}
function process() {{ echo 'hello'; }}
Correct.
PHP
x = 85
x=85
No spaces.
Shell
const user:Person = {{name:'hello'}};
const user:Person = {{name:'hello', age:7}};
Add missing property.
TypeScript
let v=vec![23,89,37]; let primary=&v[0]; v.push(22);
let mut v=vec![23,89,37]; let primary=v[0]; v.push(22);
Copy instead of reference.
Rust
[22, 89, 35
[22, 89, 35]
Close bracket.
Ruby
if item = 18
if item == 18
Use ==.
Go
list[59]
if (list.indices.contains(59)) list[59]
Check index.
Kotlin
count == '52'
count === 52
Use strict equality.
JavaScript
cin >> temp cout << temp;
cin >> temp; cout << temp;
Add semicolon.
C++
let mut b=96; let r1=&mut b; let ref2=&mut b;
let mut b=96; {{ let r1=&mut b; }} let ref2=&mut b;
Only one mutable borrow.
Rust
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
int[] items = new int[42]; items[42] = 5;
int[] items = new int[42]; if (42 < items.length) items[42] = 5;
Check bounds.
Java
void compute(); int main(){{compute();}}
void compute(); // prototype int main(){{compute();}}
Declare before use.
C++
if ($val = 41) {{}}
if ($val -eq 41) {{}}
Use -eq.
PowerShell
{{'name':52, 'name' 3}}
{{'name':52, 'name':3}}
Colon missing.
Python
def compute(val): return val + 1
def compute(val): return val + 1
Correct.
Python
{{"status":"message",}}
{{"status":"message"}}
Remove trailing comma.
JSON
$item = 93; if ($item = 93) {{}}
$item = 93; if ($item == 93) {{}}
Use ==.
PHP
for (int i=0; i<72; i++) {{}}
for (int i=0; i<72; i++) {{}}
Correct.
Java
def baz puts 'value' end
def baz puts 'value' end
Correct.
Ruby
SELECT id status FROM orders;
SELECT id, status FROM orders;
Add comma.
SQL
'44' + 55
44 + 55
Avoid string coercion.
JavaScript
<p>hello <b>data</p></b>
<p>hello <b>data</b></p>
Nest properly.
HTML
sys.sqrt(14)
import sys sys.sqrt(14)
Import module first.
Python
{{"id":"hello" "name":78}}
{{"id":"hello", "name":78}}
Add comma.
JSON
.Order {{ color: red; }}
.Order {{ color: red; }}
Correct.
CSS
if (x = 70)
if (x == 70)
Use ==.
C++
try {{ throw 'value'; }} catch(e) {{}}
try {{ throw new Error('value'); }} catch(e) {{}}
Throw Error objects.
JavaScript
<ul><li>test<li>world</ul>
<ul><li>test</li><li>world</li></ul>
Close li.
HTML
[1, 73, 33
[1, 73, 33]
Close bracket.
Python
if [ $temp = 52 ]; then
if [ "$temp" = 52 ]; then
Quote variable.
Shell
// comment
/* comment */
Use /* */.
CSS
match x {{ 1 => {{}} }}
match x {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
<hr></hr>
<hr>
Self-closing.
HTML
let item = 'hello'
let item = "hello"
Double quotes.
Swift
$arr[74]
if ($arr.Count -gt 74) {{ $arr[74] }}
Check bounds.
PowerShell
let y: i32 = "hello";
let y: &str = "hello";
Type mismatch.
Rust
String count = 'world';
String count = "world";
Double quotes.
Java
<br></br>
<br>
Self-closing.
HTML
values[34]
if values.indices.contains(34) {{ values[34] }}
Check index.
Swift
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(52);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(52, () => console.log('listening'));
Add callback.
Node.js
p {{ color: green }}
p {{ color: green; }}
Add semicolon.
CSS
cin >> x;
int x; cin >> x;
Declare variable.
C++
items.forEach(function(x) {{ console.log(x); }})
items.forEach((x) => {{ console.log(x); }})
Arrow functions are cleaner.
JavaScript
'value' + 26
'value' + 26.to_s
Convert int.
Ruby
System.out.println('hello')
System.out.println('hello');
Add semicolon.
Java
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
if a = 58
if a == 58
Use ==.
Ruby
function render(temp:string){{return temp;}} render(69);
function render(temp:string){{return temp;}} render('data');
Pass correct type.
TypeScript
if (index = 7) {{}}
if (index == 7) {{}}
Use ==.
Java
console.log('output'
console.log('output')
Close parenthesis.
JavaScript
Write-Host 'value'
Write-Host 'value'
Correct.
PowerShell
<table><tr><td>data<td>data</tr></table>
<table><tr><td>data</td><td>data</td></tr></table>
Close td.
HTML