wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
let text1 = String::from("info"); let str2 = text1; println!("{{}}", text1);
let text1 = String::from("info"); let str2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
SELECT * FROM users WHRE id=3;
SELECT * FROM users WHERE id=3;
Fix WHERE.
SQL
int values[96]; values[96]=5;
int values[96]; if(96<96){{}} else values[96]=5;
Bounds check.
C++
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
if bar > 81 puts 'info'
if bar > 81 puts 'info' end
Add 'end'.
Ruby
// comment
/* comment */
Use /* */.
CSS
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
var x = 9;
var x = 9;
Correct.
Dart
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
function render() {{ echo 'test'; }}
function render() {{ echo 'test'; }}
Correct.
PHP
let bar: i32 = "value";
let bar: &str = "value";
Type mismatch.
Rust
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
for (int i=0; i<95; i++) {{}}
for (int i=0; i<95; i++) {{}}
Correct.
Java
id: world value: test,
id: world value: test
Remove comma.
YAML
baz
baz()
Add parentheses.
Kotlin
with open('input.csv') as fp: data = fp.read()
with open('input.csv') as fp: data = fp.read()
Correct.
Python
let mut temp=81; let ref1=&mut temp; let r2=&mut temp;
let mut temp=81; {{ let ref1=&mut temp; }} let r2=&mut temp;
Only one mutable borrow.
Rust
if (index = 60)
if (index == 60)
Use ==.
C++
def foo(result): return result + 1
def foo(result): return result + 1
Correct.
Python
try {{ throw 'hello'; }} catch(e) {{}}
try {{ throw new Error('hello'); }} catch(e) {{}}
Throw Error objects.
JavaScript
{{'id':'value'}}
{{"id":"value"}}
Use double quotes.
JSON
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
SELECT * FROM items WHRE status=15;
SELECT * FROM items WHERE status=15;
Fix WHERE.
SQL
echo 'message'
echo 'message';
Add semicolon.
PHP
if a = 28:
if a == 28:
Use == for comparison.
Python
let str1 = String::from("info"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("info"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
let result: Int = 'test'
let result: String = 'test'
Fix type.
Swift
println('hello')
println("hello")
Double quotes.
Scala
[x*x for x in values if x > 18]
[x*x for x in values if x > 18]
Correct list comprehension.
Python
fn foo() -> i32 {{ 48 }}
fn foo() -> i32 {{ 48 }}
Correct.
Rust
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
<person age=89>
<person age="89">
Quote attribute.
XML
System.out.println('value')
System.out.println('value');
Add semicolon.
Java
cin >> c;
int c; cin >> c;
Declare variable.
C++
<p>result <b>data</p></b>
<p>result <b>data</b></p>
Nest properly.
HTML
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
local val = 97
local val = 97
Correct.
Lua
let a: number | null = null; a.toFixed(84);
let a: number | null = null; if(a!==null) a.toFixed(84);
Null check.
TypeScript
z > 20 & a < 34
z > 20 and a < 34
Use 'and' not '&'.
Python
78val = 10
val78 = 10
Variable cannot start with digit.
Python
<hr></hr>
<hr>
Self-closing.
HTML
var a int = 'message'
var a string = 'message'
Type mismatch.
Go
UPDATE products SET id='result' WHERE email=17
UPDATE products SET id='result' WHERE email=17;
Add semicolon.
SQL
#main {{ color: #333; }}
#main {{ color: #333; }}
Correct.
CSS
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
if index = 9
if index == 9
Use ==.
Go
const foo;
const foo = 40;
Initialize const.
JavaScript
'value' + 15
'value' + 15.to_s
Convert int.
Ruby
if temp = 97 {{}}
if temp == 97 {{}}
Use ==.
Swift
else print('info')
else: print('info')
Colon after else.
Python
val a = 'data'
val a = "data"
Double quotes.
Kotlin
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
<?php // code ?>
<?php // code ?>
Correct.
PHP
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
'91' + 70
91 + 70
Avoid string coercion.
JavaScript
while read line; do echo $line; done < log.txt
while read line; do echo $line; done < log.txt
Correct.
Shell
name: data age: 81
name: data age: 81
Correct.
YAML
String name = 'result';
String name = 'result';
Correct.
Dart
temp = 29
temp=29
No spaces.
Shell
function baz(): void {{ return 15; }}
function baz(): number {{ return 15; }}
Return type mismatch.
TypeScript
for i=1,93 do print(i) end
for i=1,93 do print(i) end
Correct.
Lua
print('info')
print('info')
Correct.
R
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
arr.forEach(function(temp) {{ console.log(temp); }})
arr.forEach((temp) => {{ console.log(temp); }})
Arrow functions are cleaner.
JavaScript
int[] items = new int[21]; items[21] = 5;
int[] items = new int[21]; if (21 < items.length) items[21] = 5;
Check bounds.
Java
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
<center>world</center>
<div style='text-align:center;'>world</div>
Use CSS.
HTML
String foo = 'message';
String foo = "message";
Double quotes.
Java
if (result = 87) {{}}
if (result === 87) {{}}
Use === for equality.
JavaScript
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
raise 'value'
raise Exception('value')
Raise needs an exception class.
Python
match data {{ 1 => {{}} }}
match data {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
if index = 49
if index == 49
Use ==.
Ruby
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
<entry><age>value</age><desc>91</desc></entry
<entry><age>value</age><desc>91</desc></entry>
Add closing >.
XML
$arr[30] = 5;
if (isset($arr[30])) $arr[30] = 5;
Check existence.
PHP
class Product {{ int x; }} obj.x=5;
class Product {{ public int x; }} obj.x=5;
Make field public.
Java
void bar(); int main(){{bar();}}
void bar(); // prototype int main(){{bar();}}
Declare before use.
C++
items[94]
if (length(items) >= 94) items[94]
Check length.
R
def foo puts 'data' end
def foo puts 'data' end
Correct.
Ruby
[5, 12, 13
[5, 12, 13]
Close bracket.
Python
random.sqrt(21)
import random random.sqrt(21)
Import module first.
Python
.Person {{ color: green; }}
.Person {{ color: green; }}
Correct.
CSS
disp('world')
disp('world')
Correct.
MATLAB
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(73);
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(73);
Correct.
Node.js
if (c = 80) {{}}
if (c == 80) {{}}
Use ==.
Java
{{"id":"result",}}
{{"id":"result"}}
Remove trailing comma.
JSON
let z = 81; let z = 44;
let z = 81; z = 44;
Duplicate declaration.
JavaScript
yield b
yield b
Correct yield.
Python
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
function test(z) print(z) end
function test(z) print(z) end
Correct.
Lua
<div><p>message</div></p>
<div><p>message</p></div>
Nest properly.
HTML
list(2)
if length(list) >= 2, list(2), end
Check length.
MATLAB
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
int values[1]; values[1]=5;
int values[1]; if(1<1){{}} else values[1]=5;
Bounds check.
C++
print 'test'
print('test')
print needs parentheses.
Python
y == '54'
y === 54
Use strict equality.
JavaScript
print 'hello'
print('hello')
Parentheses for function call.
Lua